Skip to content

Commit

Permalink
trying some pretrained models
Browse files Browse the repository at this point in the history
  • Loading branch information
jxu7 committed Jun 1, 2017
1 parent c0ab8c3 commit 12d2561
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 79 deletions.
4 changes: 2 additions & 2 deletions datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def validation_jpg_loader(batch_size=64, transform=ToTensor()):
mode='Validation',
input_transform=transform
)
return DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True)
return DataLoader(dataset=dataset, batch_size=batch_size, shuffle=False)


def test_jpg_loader(batch_size=128, transform=ToTensor()):
Expand All @@ -196,7 +196,7 @@ def test_jpg_loader(batch_size=128, transform=ToTensor()):
mode='Test',
input_transform=transform
)
return DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True)
return DataLoader(dataset=dataset, batch_size=batch_size, shuffle=False)


if __name__ == '__main__':
Expand Down
9 changes: 3 additions & 6 deletions planet_models/simplenet_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@ def __init__(self, inpt_channel, output_channel):
stride=1)
self.bn1 = nn.BatchNorm2d(output_channel//2)
self.elu1 = nn.ELU()
self.dropout_1 = nn.Dropout2d()

self.conv2 = nn.Conv2d(in_channels=output_channel//2, out_channels=output_channel//2, bias=False,
kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(output_channel//2)
self.elu2 = nn.ELU()
self.dropout_2 = nn.Dropout2d()

self.conv3 = nn.Conv2d(in_channels=output_channel//2, out_channels=output_channel, kernel_size=1, bias=False,
stride=1)
self.bn3 = nn.BatchNorm2d(output_channel)
self.elu3 = nn.ELU()
self.dropout_3 = nn.Dropout2d()

self.stage = nn.Sequential(
self.conv1, self.bn1, self.elu1,
self.conv2, self.bn2, self.elu2,
self.conv2, self.bn2, self.elu2, nn.Dropout2d(0.2),
self.conv3, self.bn3, self.elu3
)

Expand All @@ -36,7 +33,7 @@ def forward(self, x):


class TransitionBlock(nn.Module):
def __init__(self, size, input_channel, output_channel=1024):
def __init__(self, size, input_channel, output_channel=2048):
super(TransitionBlock, self).__init__()
self.conv = nn.Conv2d(input_channel, output_channel, stride=1, kernel_size=1, bias=False)
self.bn = nn.BatchNorm2d(output_channel)
Expand Down Expand Up @@ -68,7 +65,7 @@ def __init__(self, inpt_size=72, input_channel=3):
self.maxpool_4 = nn.MaxPool2d(kernel_size=2, stride=2)
self.transition_2 = TransitionBlock(4, 1024)
# (1024, 4, 4)
self.stage_4 = Block(inpt_channel=1024, output_channel=1024)
self.stage_4 = Block(inpt_channel=1024, output_channel=2048)
self.avg_pool = nn.AvgPool2d(4)
# (2048, 1, 1)
self.classifier = nn.Sequential(
Expand Down
131 changes: 131 additions & 0 deletions planet_models/simplenet_v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import torch.nn as nn
import torch
import math

"""
A 17 layer network
"""


class Block(nn.Module):
def __init__(self, inpt_channel, output_channel):
super(Block, self).__init__()
self.conv1 = nn.Conv2d(in_channels=inpt_channel, kernel_size=1, out_channels=output_channel//2, bias=False,
stride=1)
self.bn1 = nn.BatchNorm2d(output_channel//2)
self.elu1 = nn.ELU()

self.conv2 = nn.Conv2d(in_channels=output_channel//2, out_channels=output_channel//2, bias=False,
kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(output_channel//2)
self.elu2 = nn.ELU()

self.conv3 = nn.Conv2d(in_channels=output_channel//2, out_channels=output_channel, kernel_size=1, bias=False,
stride=1)
self.bn3 = nn.BatchNorm2d(output_channel)
self.elu3 = nn.ELU()

self.stage = nn.Sequential(
self.conv1, self.bn1, self.elu1,
self.conv2, self.bn2, self.elu2, nn.Dropout2d(0.2),
self.conv3, self.bn3, self.elu3
)

def forward(self, x):
return self.stage(x)


class TransitionBlock(nn.Module):
def __init__(self, size, input_channel, output_channel=2048):
super(TransitionBlock, self).__init__()
self.conv = nn.Conv2d(input_channel, output_channel, stride=1, kernel_size=1, bias=False)
self.bn = nn.BatchNorm2d(output_channel)
self.elu = nn.ELU()
self.avg_pool = nn.AvgPool2d(kernel_size=size)

def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.elu(x)
x = self.avg_pool(x)
return x


class SimpleNetV3(nn.Module):
def __init__(self, inpt_size=72, input_channel=3):
super(SimpleNetV3, self).__init__()
self.pre_layer = nn.Sequential(
self._make_conv_bn_elu(3, 16),
self._make_conv_bn_elu(16, 16),
self._make_conv_bn_elu(16, 16)
)
self.conv1 = nn.Conv2d(in_channels=16, out_channels=64, kernel_size=3, stride=1)
self.maxpool_1 = nn.MaxPool2d(kernel_size=2, stride=2)
# (64, 36, 36)
self.stage_1 = Block(inpt_channel=64, output_channel=256)
self.maxpool_2 = nn.MaxPool2d(kernel_size=2, stride=2)
# self.transition_0 = TransitionBlock(18, 256, 2048)
# (256, 18, 18)
self.stage_2 = Block(inpt_channel=256, output_channel=512)
self.maxpool_3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.transition_1 = TransitionBlock(8, 512)
# (512, 8, 8)
self.stage_3 = Block(inpt_channel=512, output_channel=1024)
self.maxpool_4 = nn.MaxPool2d(kernel_size=2, stride=2)
self.transition_2 = TransitionBlock(4, 1024)
# (1024, 4, 4)
self.stage_4 = Block(inpt_channel=1024, output_channel=2048)
self.avg_pool = nn.AvgPool2d(4)
# (2048, 1, 1)
self.classifier = nn.Sequential(
nn.Linear(2048*3, 512, bias=False),
nn.BatchNorm1d(512),
nn.ELU(),
nn.Dropout(p=0.5),
nn.Linear(512, 17)
)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()

def _make_conv_bn_elu(self, in_channels, out_channels):
layer = nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, stride=1, padding=0, kernel_size=1),
nn.BatchNorm2d(out_channels),
nn.ELU()
)

return layer

def forward(self, x):
x = self.pre_layer(x)

x = self.conv1(x)
x = self.maxpool_1(x)

# stage 1
x = self.stage_1(x)
x = self.maxpool_1(x)
# transition_0 = self.transition_0(x)

# stage 2
x = self.stage_2(x)
x = self.maxpool_2(x)
transition_1 = self.transition_1(x)

# stage 3
x = self.stage_3(x)
x = self.maxpool_3(x)
transition_2 = self.transition_2(x)

# stage 4
x = self.stage_4(x)
x = self.avg_pool(x)
x = torch.cat([x, transition_1, transition_2], -1)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
32 changes: 21 additions & 11 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,33 @@
from labels import *
from planet_models.simplenet import MultiLabelCNN
from planet_models.simplenet_v2 import SimpleNetV2
from trainers.train_densenet import densenet121
from planet_models.simplenet_v3 import SimpleNetV3
from planet_models.resnet_planet import *
from trainers.train_simplenet import evaluate
from util import BEST_THRESHOLD


MODEL='models/simplenet_v2.pth'
MODEL='models/densenet121.pth'


def test(model_dir, transform):
name = model_dir.split('/')[-1][:-4]
test_loader = test_jpg_loader(256, transform=Compose(
test_loader = test_jpg_loader(512, transform=Compose(
[
Scale(72),
Scale(224),
ToTensor(),
Normalize(mean, std)
]
))

if 'resnet' in model_dir:
model = nn.DataParallel(resnet14_planet())
elif 'resnext' in model_dir:
model = nn.DataParallel(resnext_29())
else:
model = nn.DataParallel(SimpleNetV2())
# if 'resnet' in model_dir:
# model = nn.DataParallel(resnet14_planet())
# elif 'resnext' in model_dir:
# model = nn.DataParallel(resnext_29())
# else:
# model = nn.DataParallel(SimpleNetV3())
model = nn.DataParallel(densenet121())
model.load_state_dict(torch.load(model_dir))
model.eval()

Expand All @@ -44,8 +48,14 @@ def test(model_dir, transform):
result = F.sigmoid(result)
result = result.data.cpu().numpy()
for r, id in zip(result, im_ids):
r = np.where(r >= 0.2)[0]
labels = [idx_to_label[index] for index in r]
label = np.zeros_like(r)
for i in range(17):
label[i] = (r[i] > BEST_THRESHOLD[i]).astype(np.int)
label = np.where(label == 1)[0]
labels = [idx_to_label[index] for index in label]
if len(r) == 0:
print('id', id)
print('r', r)
imid_to_label[id] = sorted(labels)
print('Batch Index {}'.format(batch_idx))
sample_submission = pd.read_csv('/media/jxu7/BACK-UP/Data/AmazonPlanet/sample_submission.csv')
Expand Down
74 changes: 74 additions & 0 deletions test_ensembles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pandas as pd
from torch.nn import functional as F
from torchvision.transforms import *
from util import BEST_THRESHOLD
from datasets import test_jpg_loader, mean, std
from labels import *
from planet_models.simplenet_v3 import SimpleNetV3
from trainers.train_densenet import densenet121
from planet_models.resnet_planet import *
from trainers.train_simplenet import evaluate


SIMPLENET = 'models/simplenet_v3.1.pth'
RESNET = 'models/densenet121.pth'

def test():
resnet = nn.DataParallel(densenet121().cuda())
resnet.load_state_dict(torch.load(RESNET))
resnet.eval()

simple_v2 = nn.DataParallel(SimpleNetV3().cuda())
simple_v2.load_state_dict(torch.load(SIMPLENET))
simple_v2.eval()

name = 'ensembles_simple_v3.1_densenet121'
resnet_loader = test_jpg_loader(512, transform=Compose(
[
Scale(224),
ToTensor(),
Normalize(mean, std)
]
))

simple_v2_loader = test_jpg_loader(512, transform=Compose(
[
Scale(72),
ToTensor(),
Normalize(mean, std)
]
))



imid_to_label = {}
for batch_idx, ((resnet_img, resim_ids), (simplenet_img, testim_ids)) in enumerate(zip(resnet_loader, simple_v2_loader)):
resnet_result = evaluate(resnet, resnet_img)
# resnet_result = F.sigmoid(resnet_result)
# resnet_result = resnet_result.data.cpu().numpy()

simplenet_result = evaluate(simple_v2, simplenet_img)
# simplenet_result = F.sigmoid(simplenet_result)

result = F.sigmoid((simplenet_result + resnet_result) / 2)
result = result.data.cpu().numpy()
for r, id in zip(result, testim_ids):
label = np.zeros_like(r)
for i in range(17):
label[i] = (r[i] > BEST_THRESHOLD[i]).astype(np.int)
label = np.where(label == 1)[0]
labels = [idx_to_label[index] for index in label]
if len(r) == 0:
print('id', id)
print('r', r)

imid_to_label[id] = sorted(labels)
print('Batch Index {}'.format(batch_idx))
sample_submission = pd.read_csv('/media/jxu7/BACK-UP/Data/AmazonPlanet/sample_submission.csv')
for key, value in imid_to_label.items():
sample_submission.loc[sample_submission['image_name'] == key,'tags'] = ' '.join(str(x) for x in value)
sample_submission.to_csv('submissions/%s.csv' % name, index=False)


if __name__ == '__main__':
test()
Loading

0 comments on commit 12d2561

Please sign in to comment.