-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jxu7
committed
Jun 1, 2017
1 parent
c0ab8c3
commit 12d2561
Showing
9 changed files
with
507 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.