From 8d3ff259274f796d801ad448354523d9f7965d7e Mon Sep 17 00:00:00 2001 From: jxu7 Date: Thu, 11 May 2017 15:15:35 -0400 Subject: [PATCH] added resnext --- planet_models/resnext.py | 20 ++++++++++++- test.py | 6 ++-- trainers/threshold.py | 5 ++++ .../{resnet_forest.py => train_resnet.py} | 28 +++++++++---------- util.py | 11 +++++--- 5 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 trainers/threshold.py rename trainers/{resnet_forest.py => train_resnet.py} (100%) diff --git a/planet_models/resnext.py b/planet_models/resnext.py index 8b35566..bcc9462 100644 --- a/planet_models/resnext.py +++ b/planet_models/resnext.py @@ -9,8 +9,26 @@ class Bottleneck(Module): """Type C in the paper""" - def __init__(self, width, planes, cardinality, downsample=None, activation_fn=ELU): + def __init__(self, inplanes, planes, cardinality, stride, downsample=None, activation_fn=ELU): + """ + Parameters: + inplanes: # of input channels + planes: # of output channels + cardinality: # of convolution groups + stride: convolution stride + downsample: convolution operation to increase the width of the output + activation_fn: activation function + """ super(Bottleneck, self).__init__() + depth = planes/2 + self.conv1 = Conv2d(inplanes, depth, kernel_size=1, stride=1, padding=0, bias=False) + self.bn1 = BatchNorm2d(planes/2) + # group convolution + self.conv2 = Conv2d(depth, depth, kernel_size=3, groups=cardinality, stride=stride, padding=1, bias=False) + self.bn2 = BatchNorm2d(planes/2) + # increase depth + self.conv3 = Conv2d(planes/2, planes, kernel_size=1, stride=1, padding=0, bias=False) + self.bn3 = BatchNorm2d(planes) def forward(self, x): pass diff --git a/test.py b/test.py index b3832b5..09db84b 100644 --- a/test.py +++ b/test.py @@ -12,7 +12,7 @@ from planet_models.resnet_planet import * from trainers.train_simplenet import evaluate -MODEL='models/resnet-101.pth' +MODEL='models/resnet-34.pth' def test(model_dir, transform): @@ -25,7 +25,7 @@ def test(model_dir, transform): )) if 'resnet' in model_dir: - model = nn.DataParallel(resnet101_planet()) + model = nn.DataParallel(resnet34_planet()) else: model = MultiLabelCNN(17) model.load_state_dict(torch.load(model_dir)) @@ -39,7 +39,7 @@ 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.15)[0] + r = np.where(r >= 0.24)[0] labels = [idx_to_label[index] for index in r] imid_to_label[id] = sorted(labels) print('Batch Index {}'.format(batch_idx)) diff --git a/trainers/threshold.py b/trainers/threshold.py new file mode 100644 index 0000000..68b036c --- /dev/null +++ b/trainers/threshold.py @@ -0,0 +1,5 @@ +from util import optimize_threshold +from planet_models.resnet_planet import resnet34_planet + +model = resnet34_planet() +optimize_threshold(model, '../models/resnet-34.pth') diff --git a/trainers/resnet_forest.py b/trainers/train_resnet.py similarity index 100% rename from trainers/resnet_forest.py rename to trainers/train_resnet.py index 5a86444..6e29bd0 100644 --- a/trainers/resnet_forest.py +++ b/trainers/train_resnet.py @@ -32,6 +32,20 @@ def train_resnet_forest(epoch=50): best_loss = np.inf patience = 0 for i in range(epoch): + # training + for batch_index, (target_x, target_y) in enumerate(train_data_set): + if is_cuda_availible: + target_x, target_y = target_x.cuda(), target_y.cuda() + resnet.train() + target_x, target_y = Variable(target_x), Variable(target_y) + optimizer.zero_grad() + output = resnet(target_x) + loss = criterion(output, target_y) + loss.backward() + optimizer.step() + + print('Finished epoch {}'.format(i)) + # evaluating val_loss = 0.0 f2_scores = 0.0 @@ -62,20 +76,6 @@ def train_resnet_forest(epoch=50): logger.add_record('train_loss', loss.data[0]) logger.add_record('evaluation_loss', val_loss.data[0]/batch_index) logger.add_record('f2_score', f2_scores/batch_index) - - # training - for batch_index, (target_x, target_y) in enumerate(train_data_set): - if is_cuda_availible: - target_x, target_y = target_x.cuda(), target_y.cuda() - resnet.train() - target_x, target_y = Variable(target_x), Variable(target_y) - optimizer.zero_grad() - output = resnet(target_x) - loss = criterion(output, target_y) - loss.backward() - optimizer.step() - - print('Finished epoch {}'.format(i)) logger.save() logger.save_plot() diff --git a/util.py b/util.py index d94f326..1f9da13 100644 --- a/util.py +++ b/util.py @@ -75,8 +75,11 @@ def optimize_threshold(model, mode_dir, resolution=10000): """ model = nn.DataParallel(model) model.load_state_dict(torch.load(mode_dir)) - model.cuda(0) - data = validation_jpg_loader(256, transform=input_transform(227)) + model.cuda() + data = validation_jpg_loader(256, transform=Compose([ + Scale(224), + ToTensor() + ])) num_class = 17 pred = [] targets = [] @@ -130,9 +133,9 @@ def save_plot(self): plt.plot(np.arange(len(eval_loss)), eval_loss, color='blue', label='eval_loss') plt.legend(loc='best') - plt.savefig('log/%s_losses.jpg' % self.name) + plt.savefig('../log/%s_losses.jpg' % self.name) plt.figure() plt.plot(np.arange(len(f2_scores)), f2_scores) - plt.savefig('log/%s_fcscore.jpg' % self.name) + plt.savefig('../log/%s_fcscore.jpg' % self.name)