This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Base length subtraction has to subtract file extension then re-add after | |
Dir | Rename-Item -NewName {$_.name.substring(3,$_.BaseName.length-4) + '.png'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.colab.output import eval_js | |
from base64 import b64decode | |
VIDEO_HTML = """ | |
<video autoplay | |
width=800 height=600></video> | |
<script> | |
var video = document.querySelector('video') | |
navigator.mediaDevices.getUserMedia({ video: true }) | |
.then(stream=> video.srcObject = stream) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DQN(nn.Module): | |
def __init__(self, input_shape, n_actions): | |
super(DQN, self).__init__() | |
self.conv = nn.Sequential( | |
nn.Conv2d(input_shape[0], 32, kernel_size=8, stride=4), | |
nn.ReLU(), | |
nn.Conv2d(32, 64, kernel_size=4, stride=2), | |
nn.ReLU(), | |
nn.Conv2d(64, 64, kernel_size=3, stride=1), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
from torch.autograd import Variable | |
import numpy as np | |
import cv2 | |
def predict_transform(prediction, inp_dim, anchors, num_classes, CUDA = True): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get the "features" portion of VGG19 | |
# The classifier portion is not needed for style transfer | |
vgg = models.vgg19(pretrained=True).features | |
# Freeze all VGG parameters | |
# Since only activations are measured for each conv layer | |
# Network weights aren't modified | |
for param in vgg.parameters(): | |
param.requires_grad_(False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
show_every = 400 # Show target image every x steps | |
optimizer = optim.Adam([target], lr=0.003) # Optimizer hyperparameters | |
steps = 2000 # How many iterations to update content image | |
# Training Loop | |
for ii in range(steps): | |
# Calculate the content loss | |
target_features = get_features(target, vgg) | |
content_loss = torch.mean((target_features['conv4_2'] - content_features['conv4_2'])**2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gram_matrix(tensor): | |
# Get the batch_size, depth, height, and width of the Tensor | |
# Reshape it, so we're multiplying the features for each channel | |
_, d, h, w = tensor.size() | |
tensor = tensor.view(d, h*w) | |
# Calculate the gram matrix by multiplying the tensor with its transpose | |
gram = torch.mm(tensor, tensor.t()) | |
return gram |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate the average squared error between two high-level layer activations | |
content_loss = torch.mean((target_features['conv4_2'] - content_features['conv4_2'])**2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
data = {} | |
# Read from disk | |
pickle_jar = open('data.pkl', 'rb') | |
data = pickle.load(pickle_jar) | |
# Write to disk | |
pickle_jar = open('data.pkl', 'wb') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
# Read variables from disk | |
with open('data.json', 'r') as json_file: | |
data = json.load(json_file) | |
a = data['a'] | |
b = data['b'] | |
c = data['c'] | |
d = data['d'] |