Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
import os
import torch
import pandas as pd
import numpy as np
from torch.utils.data import Dataset, random_split, DataLoader
from PIL import Image
import torchvision.models as models
import matplotlib.pyplot as plt
from tqdm.notebook import tqdm
import torchvision.transforms as T
from sklearn.metrics import f1_score
import torch.nn.functional as F
import torch.nn as nn
from torchvision.utils import make_grid
%matplotlib inline
Configuration Files
DATA_DIR = '../input/jovian-pytorch-z2g/Human protein atlas'
TRAIN_DIR = DATA_DIR + '/train'
TEST_DIR = DATA_DIR + '/test'
TRAIN_CSV = DATA_DIR + '/train.csv'
TEST_CSV = '../input/jovian-pytorch-z2g/submission.csv'
data_df = pd.read_csv(TRAIN_CSV)
print(data_df.head())
labels = {
0: 'Mitochondria',
1: 'Nuclear bodies',
2: 'Nucleoli',
3: 'Golgi apparatus',
4: 'Nucleoplasm',
5: 'Nucleoli fibrillar center',
6: 'Cytosol',
7: 'Plasma membrane',
8: 'Centrosome',
9: 'Nuclear speckles'
}
Image Label
0 19567 9
1 29993 6 4
2 17186 1 4
3 29600 6 2
4 701 3 4
Utility Functions
def encode_label(label):
target = torch.zeros(10)
for l in str(label).split(' '):
target[int(l)] = 1.
return target
def decode_target(target, text_labels=False, threshold=0.5):
result = []
for i, x in enumerate(target):
if (x >= threshold):
if text_labels:
result.append(labels[i] + "(" + str(i) + ")")
else:
result.append(str(i))
return ' '.join(result)
def show_sample(img, target, invert=True):
if invert:
plt.imshow(1 - img.permute((1, 2, 0)))
else:
plt.imshow(img.permute(1, 2, 0))
print('Labels:', decode_target(target, text_labels=True))
def show_batch(dl, invert=True):
for images, labels in dl:
fig, ax = plt.subplots(figsize=(16, 8))
ax.set_xticks([]); ax.set_yticks([])
data = 1-images if invert else images
ax.imshow(make_grid(data, nrow=16).permute(1, 2, 0))
break