File size: 5,762 Bytes
5980447
1
2
{"repo": "wkentaro/fcn", "pull_number": 21, "instance_id": "wkentaro__fcn-21", "issue_numbers": "", "base_commit": "c3fd7f6e1ee1547df3bd4b11b6486915911a8c28", "patch": "diff --git a/fcn/models/fcn8s.py b/fcn/models/fcn8s.py\n--- a/fcn/models/fcn8s.py\n+++ b/fcn/models/fcn8s.py\n@@ -113,13 +113,7 @@ def __call__(self, x, t=None):\n         upscore2 = h  # 1/16\n \n         # score_pool4c\n-        # TODO(pfnet): Implement crop function\n-        # h = F.crop(score_pool4, upscore2, axis=2, offset=5)\n-        h = score_pool4\n-        for axis in [2, 3]:\n-            start = 5\n-            end = start + upscore2.data.shape[axis]\n-            _, h, _  = F.split_axis(h, [start, end], axis=axis)\n+        h = F.crop(score_pool4, upscore2, axes=[2, 3], offset=5)\n         score_pool4c = h  # 1/16\n \n         # fuse_pool4\n@@ -131,13 +125,7 @@ def __call__(self, x, t=None):\n         upscore_pool4 = h  # 1/8\n \n         # score_pool4c\n-        # TODO(pfnet): Implement crop function\n-        # h = F.crop(score_pool3, upscore_pool4, axis=2, offset=9)\n-        h = score_pool3\n-        for axis in [2, 3]:\n-            start = 9\n-            end = start + upscore_pool4.data.shape[axis]\n-            _, h, _ = F.split_axis(h, [start, end], axis=axis)\n+        h = F.crop(score_pool3, upscore_pool4, axes=[2, 3], offset=9)\n         score_pool3c = h  # 1/8\n \n         # fuse_pool3\n@@ -149,13 +137,7 @@ def __call__(self, x, t=None):\n         upscore8 = h  # 1/1\n \n         # score\n-        # TODO(pfnet): Implement crop function\n-        # h = F.crop(upscore8, x, axis=2, offset=31)\n-        h = upscore8\n-        for axis in [2, 3]:\n-            start = 31\n-            end = start + x.data.shape[axis]\n-            _, h, _ = F.split_axis(h, [start, end], axis=axis)\n+        h = F.crop(upscore8, x, axes=[2, 3], offset=31)\n         score = h  # 1/1\n \n         # testing without t\n@@ -175,5 +157,6 @@ def accuracy_score(self, y_pred, y_true):\n         y_true = cuda.to_cpu(y_true.data)\n         # reduce values along classes axis\n         reduced_y_pred = np.argmax(y_pred, axis=1)\n-        s = (reduced_y_pred == y_true).mean()\n-        return s\n+        assert reduced_y_pred.shape == y_true.shape\n+        score = (reduced_y_pred == y_true).mean()\n+        return score\ndiff --git a/scripts/fcn_train.py b/scripts/fcn_train.py\n--- a/scripts/fcn_train.py\n+++ b/scripts/fcn_train.py\n@@ -10,7 +10,7 @@\n import chainer.serializers as S\n from chainer import Variable\n import numpy as np\n-import progressbar\n+import tqdm\n \n import fcn\n from fcn.models import FCN8s\n@@ -22,6 +22,7 @@ class Trainer(object):\n \n     def __init__(self, gpu):\n         self.gpu = gpu\n+        self.epoch = 0\n         # pretrained model\n         pretrained_model = self._setup_pretrained_model()\n         # dataset\n@@ -54,7 +55,7 @@ def batch_loop(self, type):\n \n         Args:\n \n-            - type (str): train, trainval, or val\n+            - type (str): 'train' or 'val'\n \n         .. note::\n \n@@ -63,11 +64,12 @@ def batch_loop(self, type):\n         self.model.train = True if type == 'train' else False\n         N_data = len(self.dataset[type])\n         sum_loss, sum_accuracy = 0, 0\n-        pbar = progressbar.ProgressBar(max_value=N_data)\n-        for i in xrange(0, N_data):\n-            pbar.update(i+1)\n+        desc = 'epoch{0}: {1} batch_loop'.format(self.epoch, type)\n+        batch_size = 1\n+        assert batch_size == 1  # FCN8s only supports 1 size batch\n+        for i in tqdm.tqdm(xrange(0, N_data, batch_size), ncols=80, desc=desc):\n             # load batch\n-            batch = self.dataset.next_batch(batch_size=1, type=type)\n+            batch = self.dataset.next_batch(batch_size=batch_size, type=type)\n             img, label = batch.img[0], batch.label[0]\n             # x\n             x_datum = self.dataset.img_to_datum(img)\n@@ -86,9 +88,8 @@ def batch_loop(self, type):\n                 self.optimizer.update(self.model, x, y)\n             else:\n                 self.model(x, y)\n-            sum_loss += cuda.to_cpu(self.model.loss.data) * len(batch)\n-            sum_accuracy += self.model.accuracy * len(batch)\n-        pbar.finish()\n+            sum_loss += cuda.to_cpu(self.model.loss.data) * batch_size\n+            sum_accuracy += self.model.accuracy * batch_size\n         mean_loss = sum_loss / N_data\n         mean_accuracy = sum_accuracy / N_data\n         return mean_loss, mean_accuracy\n@@ -96,7 +97,8 @@ def batch_loop(self, type):\n     def main_loop(self):\n         log_csv = osp.join(fcn.get_data_dir(), 'log.csv')\n         for epoch in xrange(100):\n-            for type in ['train', 'trainval', 'val']:\n+            self.epoch = epoch\n+            for type in ['train', 'val']:\n                 mean_loss, mean_accuracy = self.batch_loop(type=type)\n                 log = dict(epoch=epoch, type=type, loss=mean_loss,\n                            accuracy=mean_accuracy)\n@@ -104,6 +106,12 @@ def main_loop(self):\n                       'mean_accuracy: {accuracy}'.format(**log))\n                 with open(log_csv, 'a') as f:\n                     f.write('{epoch},{type},{loss},{accuracy}\\n'.format(**log))\n+            if epoch % 10 == 0:\n+                data_dir = fcn.get_data_dir()\n+                chainermodel = osp.join(data_dir, 'fcn8s_{0}.chainermodel'.format(epoch))\n+                optimizer_file = osp.join(data_dir, 'fcn8s_{0}.adam'.format(epoch))\n+                S.save_hdf5(chainermodel, self.model)\n+                S.save_hdf5(optimizer_file, self.optimizer)\n \n \n if __name__ == '__main__':\n", "test_patch": "", "problem_statement": "", "hints_text": "", "created_at": "2016-05-08T18:40:02Z"}