-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
143 lines (114 loc) · 4.71 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
from glob import glob
import seaborn as sns
from torch.utils import data
import torch
from PIL import Image
torch.manual_seed(42)
np.random.seed(42)
#read and import dataset
base_skin_dir = '/Users/howardhuang/Documents/TopDoc/skin-cancer-mnist-ham10000/HAM10000_images_part_1'
imageid_path_dict = {os.path.splitext(os.path.basename(x))[0]: x
for x in glob(os.path.join(base_skin_dir, '*.jpg'))}
#print("dictionary: ")
#print(imageid_path_dict)
lesion_type_dict = {
'nv': 'Melanocytic nevi',
'mel': 'dermatofibroma',
'bkl': 'Benign keratosis-like lesions ',
'bcc': 'Basal cell carcinoma',
'akiec': 'Actinic keratoses',
'vasc': 'Vascular lesions',
'df': 'Dermatofibroma'
}
tile_df = pd.read_csv('/Users/howardhuang/Documents/TopDoc/skin-cancer-mnist-ham10000/HAM10000_metadata.csv')
tile_df['path'] = tile_df['image_id'].map(imageid_path_dict.get)
#print(tile_df['path'])
tile_df['cell_type'] = tile_df['dx'].map(lesion_type_dict.get)
tile_df['cell_type_idx'] = pd.Categorical(tile_df['cell_type']).codes
tile_df[['cell_type_idx', 'cell_type']].sort_values('cell_type_idx').drop_duplicates()
#counts number of each type of tumor in dataset
tile_df['cell_type'].value_counts()
#print(tile_df.sample(3))
#load in a pretrained ResNet50 model
import torchvision.models as models
model_conv = models.resnet50(pretrained=True)
#Convoluted Neural Network
#print(model_conv)
#adjust last layer of (FC) We deal with only 7 cases so chain 1000 output neurons to 7 neurons
num_ftrs = model_conv.fc.in_features
model_conv.fc = torch.nn.Linear(num_ftrs, 7)
#print(model_conv.fc)
## Linear(in_features=2048, out_features=7, bias=True)
# Define the device:
device = torch.device('cpu:0')
# Put the model on the device:
model = model_conv.to(device)
from sklearn.model_selection import train_test_split
train_df, test_df = train_test_split(tile_df, test_size=0.1)
# We can split the test set again in a validation set and a true test set:
validation_df, test_df = train_test_split(test_df, test_size=0.5)
train_df = train_df.reset_index()
validation_df = validation_df.reset_index()
test_df = test_df.reset_index()
#print("----TRAINDF---")
#print(train_df)
class Dataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, df, transform=None):
'Initialization'
self.df = df
self.transform = transform
def __len__(self):
'Denotes the total number of samples'
return len(self.df)
def __getitem__(self, index):
'Generates one sample of data'
# Load data and get label
X = Image.open(open(self.df['path'][index], 'rb'))
y = torch.tensor(int(self.df['cell_type_idx'][index]))
if self.transform:
X = self.transform(X)
return X, y
# Define the parameters for the dataloader
params = {'batch_size': 4,
'shuffle': True,
'num_workers': 6}
# define the transformation of the images.
import torchvision.transforms as trf
composed = trf.Compose([trf.RandomHorizontalFlip(), trf.RandomVerticalFlip(), trf.CenterCrop(256), trf.RandomCrop(224), trf.ToTensor(),
trf.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
# Define the trainingsset using the table train_df and using our defined transitions (composed)
training_set = Dataset(train_df, transform=composed)
training_generator = data.DataLoader(training_set, **params)
# Same for the validation set:
validation_set = Dataset(validation_df, transform=composed)
validation_generator = data.DataLoader(validation_set, **params)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-6)
criterion = torch.nn.CrossEntropyLoss()
model.eval()
test_set = Dataset(validation_df, transform=composed)
test_generator = data.SequentialSampler(validation_set)
data_sample = Image.open(open('/Users/howardhuang/Documents/TopDoc/skin-cancer-mnist-ham10000/HAM10000_images_part_1/ISIC_0024664.jpg', 'rb'))
if validation_set.transform:
data_sample = validation_set.transform(data_sample)
data_gpu = data_sample.unsqueeze(0).to(device)
output = model(data_gpu)
result = torch.argmax(output)
final = str(result)
if final == 'tensor(0)':
ret = 'POSITIVE FOR SKIN CANCER: Melanocytic nevi'
elif final == 'tensor(1)' or final == 'tensor(6)':
ret = 'POSITIVE FOR SKIN CANCER: Dermatofibroma'
elif final == 'tensor(2)':
ret = 'NEGATIVE FOR SKIN CANCER: Benign keratosis-like lesions'
elif final == 'tensor(3)':
ret = 'POSITIVE FOR SKIN CANCER: Basal cell carcinoma'
elif final == 'tensor(4)':
ret = 'POSITIVE FOR SKIN CANCER: Actinic keratoses'
elif final == 'tensor(5)':
ret = 'POSITIVE FOR SKIN CANCER: Vascular lesions'
print(ret)