Skip to content

Instantly share code, notes, and snippets.

View prateekjoshi565's full-sized avatar
🎯
Focusing

Prateek Joshi prateekjoshi565

🎯
Focusing
View GitHub Profile
def create_video_from_images(images_folder, output_video, fps=10, duration=6):
images = sorted(os.listdir(images_folder))
# create path to the input images
img_path = os.path.join(images_folder, images[0])
# load image
frame = cv2.imread(img_path)
# extract dimensions of the image
height, width, channels = frame.shape
import cv2 # (OpenCV) version - 4.7.0
import os
# predict next token
def predict(net, tkn, h=None):
# tensor inputs
x = np.array([[token2int[tkn]]])
inputs = torch.from_numpy(x)
# push to GPU
inputs = inputs.cuda()
def train(net, epochs=10, batch_size=32, lr=0.001, clip=1, print_every=32):
# optimizer
opt = torch.optim.Adam(net.parameters(), lr=lr)
# loss
criterion = nn.CrossEntropyLoss()
# push model to GPU
net.cuda()
# instantiate the model
net = WordLSTM()
# push the model to GPU (avoid it if you are not using the GPU)
net.cuda()
print(net)
class WordLSTM(nn.Module):
def __init__(self, n_hidden=256, n_layers=4, drop_prob=0.3, lr=0.001):
super().__init__()
self.drop_prob = drop_prob
self.n_layers = n_layers
self.n_hidden = n_hidden
self.lr = lr
def get_batches(arr_x, arr_y, batch_size):
# iterate through the arrays
prv = 0
for n in range(batch_size, arr_x.shape[0], batch_size):
x = arr_x[prv:n,:]
y = arr_y[prv:n,:]
prv = n
yield x, y
def get_integer_seq(seq):
return [token2int[w] for w in seq.split()]
# convert text sequences to integer sequences
x_int = [get_integer_seq(i) for i in x]
y_int = [get_integer_seq(i) for i in y]
# convert lists to numpy arrays
x_int = np.array(x_int)
y_int = np.array(y_int)
# create integer-to-token mapping
int2token = {}
cnt = 0
for w in set(" ".join(movie_plots).split()):
int2token[cnt] = w
cnt+= 1
# create token-to-integer mapping
token2int = {t: i for i, t in int2token.items()}
# create inputs and targets (x and y)
x = []
y = []
for s in seqs:
x.append(" ".join(s.split()[:-1]))
y.append(" ".join(s.split()[1:]))