Skip to content

Commit

Permalink
Training with [1,2,3] channels
Browse files Browse the repository at this point in the history
  • Loading branch information
ngxbac committed Jul 3, 2019
1 parent 9c86416 commit 48038a4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bin/train.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export CUDA_VISIBLE_DEVICES=2,3
RUN_CONFIG=config.yml


LOGDIR=/raid/bac/kaggle/logs/recursion_cell/se_resnext50_32x4d/
LOGDIR=/raid/bac/kaggle/logs/recursion_cell/test/rgb/se_resnext50_32x4d/
catalyst-dl run \
--config=./configs/${RUN_CONFIG} \
--logdir=$LOGDIR \
Expand Down
4 changes: 3 additions & 1 deletion configs/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
model_params:
model: cell_senet
model_name: se_resnext50_32x4d
n_channels: 6
n_channels: 3
num_classes: 1108

args:
Expand Down Expand Up @@ -40,6 +40,8 @@ stages:
train_csv: "./csv/train_0.csv"
valid_csv: "./csv/valid_0.csv"
root: "/raid/data/kaggle/recursion-cellular-image-classification/"
site: 1
channels: [1, 2, 3]

stage1:

Expand Down
6 changes: 6 additions & 0 deletions preprocessing/combinations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from itertools import combinations

a = [1, 2, 3, 4, 5, 6]
result = [list(i) for i in combinations(a, 3)]
print(result)
print(len(result))
6 changes: 6 additions & 0 deletions preprocessing/convert_weight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import torch

import pdb
pdb.set_trace()
checkpoint = torch.load("../pretrained/results/se_resnext50.attention.per_image_norm.1024/checkpoint/swa.10.022.pth", map_location='cpu')

63 changes: 63 additions & 0 deletions src/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@

NUM_CLASSES = 1108

DEFAULT_CHANNELS = (1, 2, 3, 4, 5, 6)
RGB_MAP = {
1: {
'rgb': np.array([19, 0, 249]),
'range': [0, 51]
},
2: {
'rgb': np.array([42, 255, 31]),
'range': [0, 107]
},
3: {
'rgb': np.array([255, 0, 25]),
'range': [0, 64]
},
4: {
'rgb': np.array([45, 255, 252]),
'range': [0, 191]
},
5: {
'rgb': np.array([250, 0, 253]),
'range': [0, 89]
},
6: {
'rgb': np.array([254, 255, 40]),
'range': [0, 191]
}
}



def load_image(path):
image = cv2.imread(path, 0)
Expand Down Expand Up @@ -61,6 +90,39 @@ def load_images_as_tensor(image_paths, dtype=np.uint8):
return data


def convert_tensor_to_rgb(t, channels=DEFAULT_CHANNELS, vmax=255, rgb_map=RGB_MAP):
"""
Converts and returns the image data as RGB image
Parameters
----------
t : np.ndarray
original image data
channels : list of int
channels to include
vmax : int
the max value used for scaling
rgb_map : dict
the color mapping for each channel
See rxrx.io.RGB_MAP to see what the defaults are.
Returns
-------
np.ndarray the image data of the site as RGB channels
"""
colored_channels = []
for i, channel in enumerate(channels):
x = (t[:, :, i] / vmax) / \
((rgb_map[channel]['range'][1] - rgb_map[channel]['range'][0]) / 255) + \
rgb_map[channel]['range'][0] / 255
x = np.where(x > 1., 1., x)
x_rgb = np.array(
np.outer(x, rgb_map[channel]['rgb']).reshape(512, 512, 3),
dtype=int)
colored_channels.append(x_rgb)
im = np.array(np.array(colored_channels).sum(axis=0), dtype=int)
im = np.where(im > 255, 255, im)
return im


class RecursionCellularSite(Dataset):

def __init__(self,
Expand Down Expand Up @@ -111,6 +173,7 @@ def __getitem__(self, idx):
]

image = load_images_as_tensor(channel_paths, dtype=np.float32)
# image = convert_tensor_to_rgb(image)
image = image / 255
if self.transform:
image = self.transform(image=image)['image']
Expand Down
8 changes: 4 additions & 4 deletions src/make_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ def predict(model, loader):

def predict_all():
test_csv = '/raid/data/kaggle/recursion-cellular-image-classification/test.csv'
log_dir = "/raid/bac/kaggle/logs/recursion_cell/se_resnext50_32x4d/"
log_dir = "/raid/bac/kaggle/logs/recursion_cell/test/rgb/se_resnext50_32x4d/"
root = "/raid/data/kaggle/recursion-cellular-image-classification/"
site = 1
channels = [1,2,3,4,5,6]
channels = [1,2,3]

model = cell_senet(
model_name="se_resnext50_32x4d",
num_classes=1108,
n_channels=6
n_channels=len(channels)
)

checkpoint = f"{log_dir}/checkpoints/best.pth"
Expand Down Expand Up @@ -74,7 +74,7 @@ def predict_all():
submission = df.copy()
submission['sirna'] = all_preds.astype(int)
os.makedirs("submission", exist_ok=True)
submission.to_csv('./submission/submission_se_resnext50_32x4d.csv', index=False, columns=['id_code', 'sirna'])
submission.to_csv('./submission/submission_se_resnext50_32x4d_rgb.csv', index=False, columns=['id_code', 'sirna'])


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion src/models/senet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def cell_senet(model_name='se_resnext50', num_classes=1108, n_channels=6):
num_classes=num_classes,
pretrained=True
)
print(model)
# print(model)
conv1 = model._features[0].conv1
model._features[0].conv1 = nn.Conv2d(in_channels=n_channels,
out_channels=conv1.out_channels,
Expand Down

0 comments on commit 48038a4

Please sign in to comment.