Skip to content

Instantly share code, notes, and snippets.

View pranshuj73's full-sized avatar
💭
La poésie est dans la rue

Pranshu Jha pranshuj73

💭
La poésie est dans la rue
View GitHub Profile
JavaScript 27 mins █████████████████▎░░░ 82.3%
JSON 5 mins ███▋░░░░░░░░░░░░░░░░░ 17.8%
@pranshuj73
pranshuj73 / filename_validator.py
Created December 11, 2021 17:14
replacing extensions in file names
import os
files = os.listdir()
for file in files:
if '_11zon' in file:
print(file)
os.rename(file, file.replace('_11zon', ''))
@pranshuj73
pranshuj73 / instructions.md
Last active October 14, 2020 20:17
Instructions for making rock-paper-scissors game in python

Rock-Paper_Scissors in Python

Algorithm:

  • Collect user input and check if user's move is rock, paper or scissors and simultaneously use random library to generate a random computer move.
  • Use if-elif-else or if-else statements to compare the user's move and computer's move
    • if user's move is superior to computer's move (eg: rock > scissors and similarly paper > rock), print that user won
    • else print user lost

Things to learn to make the game:

import typing
class ArticleField:
"""The `ArticleField` class for the Advanced Requirements."""
def __init__(self, field_type: typing.Type[typing.Any]):
self.field_type = field_type
self.attribute = ''
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
while vid.isOpened():
_, frame = vid.read()
# takes in a gray coloured filter of the frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# initializing the haarcascade face detector
faces = face_cascade.detectMultiScale(frame)
# function to turn photos to tensor
def img2tensor(x):
transform = transforms.Compose(
[transforms.ToPILImage(),
transforms.Grayscale(num_output_channels=1),
transforms.ToTensor(),
transforms.Normalize((0.5), (0.5))])
return transform(x)
# the model for predicting
torch.save(model.state_dict(), 'FER2013-Resnet9.pth')
@torch.no_grad() # this is for stopping the model from keeping track of old parameters
def evaluate(model, val_loader):
# This function will evaluate the model and give back the val acc and loss
model.eval()
outputs = [model.validation_step(batch) for batch in val_loader]
return model.validation_epoch_end(outputs)
# getting the current learning rate
def get_lr(optimizer):
for param_group in optimizer.param_groups:
def conv_block(in_chnl, out_chnl, pool=False, padding=1):
layers = [
nn.Conv2d(in_chnl, out_chnl, kernel_size=3, padding=padding),
nn.BatchNorm2d(out_chnl),
nn.ReLU(inplace=True)]
if pool: layers.append(nn.MaxPool2d(2))
return nn.Sequential(*layers)
class FERModel(FERBase):
def __init__(self, in_chnls, num_cls):
def accuracy(outputs, labels):
_, preds = torch.max(outputs, dim=1)
return torch.tensor(torch.sum(preds==labels).item()/len(preds))
class FERBase(nn.Module):
# this takes is batch from training dl
def training_step(self, batch):
images, labels = batch
out = self(images) # calls the training model and generates predictions