Skip to content

Instantly share code, notes, and snippets.

View devpatelio's full-sized avatar
🏟️
!99S

Dev Patel devpatelio

🏟️
!99S
View GitHub Profile
@devpatelio
devpatelio / ERC20Token.sol
Created August 5, 2020 20:02
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity ^0.5.1;
contract ERC20Token {
string public name;
mapping(address => uint256) public balances;
constructor(string memory _token_name) public{
name = _token_name;
}
@devpatelio
devpatelio / setup.py
Last active December 27, 2020 17:53
Downloading the dataset from kaggle
## in the command-line, type 'kaggle datasets download -d sovitrath/diabetic-retinopathy-224x224-gaussian-filtered'
from __future__ import print_function, division
import os
import torch
import pandas as pd
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
trainer_names_csv = pd.read_csv('/Users/devpatelio/Downloads/Coding/Python/pyTorch/diabetic-retinopathy-224x224-gaussian-filtered/train.csv')
n = 3662
img_names = trainer_names_csv.iloc[:n, 0]
label = trainer_names_csv.iloc[:n, 1]
def showimages(img):
plt.imshow(img)
showimages(io.imread('/Users/devpatelio/Downloads/Coding/Python/pyTorch/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images/' + img_names[0] + '.png'))
from torch.utils.data import Dataset
import pandas as pd
import os
from PIL import Image
import torch
class DRDataset(Dataset):
def __init__ (self, csv_file, root_dir, transform=None):
"""
csv_file = labels
image_dataset = DRDataset(csv_file='/Users/devpatelio/Downloads/Coding/Python/pyTorch/diabetic-retinopathy-224x224-gaussian-filtered/train.csv', root_dir='/Users/devpatelio/Downloads/Coding/Python/pyTorch/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images/')
transformed_dataset = DRDataset(csv_file='/Users/devpatelio/Downloads/Coding/Python/pyTorch/diabetic-retinopathy-224x224-gaussian-filtered/train.csv', root_dir='/Users/devpatelio/Downloads/Coding/Python/pyTorch/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images/',
transform=transforms.Compose([transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]))
"""
created 2 datasets ->
transformed_dataset = transformed using the Normalize function and to a tensor dtype
image_dataset = dataset that is used to preview images
"""
from tqdm import tqdm
import random
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
num_epochs = 10
class Net(nn.Module):
def __init__ (self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 8, 5) ## nn.Conv2d(input channels (image), output channels (no. of kernels), kernel size)
self.pool = nn.MaxPool2d(2, 2) ##maxpool with kernel size 2, stride 2
self.conv2 = nn.Conv2d(8, 8, 5) ## second conv layer
self.conv3 = nn.Conv2d(8, 16, 5) ## third conv layer
self.fc1 = nn.Linear(9216, 2000) #to find the first parameter of fc1, you can uncomment the print(x.shape) in the forward pass and take the value returned by it and paste it in
self.fc2 = nn.Linear(2000, 120)
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9)
for epoch in range(num_epochs):
running_loss = 0.0
for i, data in enumerate(train_loader, 0): #starting from i=0, we iterate over the train_loader
inputs, labels = data #assign image and corresp. label to data variable
labels = labels.type(torch.LongTensor) #turn label into longtensor just in case error is thrown
optimizer.zero_grad() #zero the gradients before training begins after each epoch
outputs = net(inputs) #input the given values into the model 'net'
# print(labels.data)
# print(outputs.data)
loss = criterion(outputs, labels) #calculates the loss between the outputs and the given labels
class Net(nn.Module):
def __init__ (self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 8, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(8, 8, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv3 = nn.Conv2d(8, 20, 5) ## note change here in 16 to 20
self.pool = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(11520, 2000) ## note change here in 9216 to 11520