From 2138ad3409c6c8d07d42296ca590172c7307b31e Mon Sep 17 00:00:00 2001 From: Jenia Golbstein Date: Sat, 10 Aug 2019 09:18:10 +0300 Subject: [PATCH] Add files via upload --- segmentation.ipynb | 66 ++++++++++++++++++++++++++++++++-------------- utils.py | 12 ++++----- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/segmentation.ipynb b/segmentation.ipynb index 99085560..5d5a12f1 100644 --- a/segmentation.ipynb +++ b/segmentation.ipynb @@ -2,16 +2,23 @@ "cells": [ { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Using TensorFlow backend.\n" + ] + }, { "name": "stdout", "output_type": "stream", "text": [ - "Num workers: 8\n", + "Num workers: 6\n", "Backbone: mobilenetv2\n", "Path to dataset: /workspace/datasets/OpenSourceDatasets/VOCdevkit/VOC2012/\n", "N classes: 21\n", @@ -30,6 +37,7 @@ "bs = 2\n", "\n", "better_model = False\n", + "load_pretrained_weights = True\n", "\n", "losses = sparse_crossentropy_ignoring_last_label\n", "metrics = {'pred_mask' : [Jaccard, sparse_accuracy_ignoring_last_label]}\n", @@ -62,22 +70,39 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "WARNING:tensorflow:From C:\\Users\\Jenia\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\framework\\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Colocations handled automatically by placer.\n", + "WARNING:tensorflow:From C:\\Users\\Jenia\\Anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n", + "WARNING:tensorflow:From C:\\Users\\Jenia\\Desktop\\Keras-segmentation-deeplab-v3.1\\utils.py:110: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Use tf.cast instead.\n", + "WARNING:tensorflow:From C:\\Users\\Jenia\\Desktop\\Keras-segmentation-deeplab-v3.1\\utils.py:116: to_int64 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Use tf.cast instead.\n", + "WARNING:tensorflow:From C:\\Users\\Jenia\\Desktop\\Keras-segmentation-deeplab-v3.1\\utils.py:119: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Use tf.cast instead.\n", "Weights path: weights/mobilenetv2_original.h5\n" ] } ], "source": [ "if better_model:\n", - " model = SegClass.create_seg_model(net='subpixel',n=n_classes, load_weights=load_pretrained_weights, multi_gpu=False, backbone=backbone)\n", + " model = SegClass.create_seg_model(net='subpixel',n=n_classes, load_weights=load_pretrained_weights, \n", + " multi_gpu=False, backbone=backbone)\n", "else:\n", - " model = SegClass.create_seg_model(net='original',n=n_classes, load_weights=load_pretrained_weights, multi_gpu=False, backbone=backbone)\n", + " model = SegClass.create_seg_model(net='original',n=n_classes, load_weights=load_pretrained_weights, \n", + " multi_gpu=False, backbone=backbone)\n", " \n", "model.compile(optimizer = Adam(lr=7e-4, epsilon=1e-8, decay=1e-6), sample_weight_mode = \"temporal\",\n", " loss = losses, metrics = metrics)\n", @@ -99,13 +124,13 @@ }, "outputs": [], "source": [ - "train_generator = SegClass.create_generators(blur = 5,crop_shape = None, mode = 'train', n_classes = n_classes,\n", - " horizontal_flip = True, vertical_flip = False, brightness=0.3, \n", - " rotation=False, zoom=0.1, validation_split = .15, seed = 7, do_ahisteq = False)\n", - "valid_generator = SegClass.create_generators(blur = 0, crop_shape = None, mode = 'validation', \n", - " n_classes = n_classes, horizontal_flip = True, vertical_flip = False, \n", - " brightness=.1, rotation=False, zoom=.05, validation_split = .15, \n", - " seed = 7, do_ahisteq = False)" + "train_generator = SegClass.create_generators(blur=5,crop_shape=None, mode='train', n_classes=n_classes,\n", + " horizontal_flip=True, vertical_flip=False, brightness=0.3, \n", + " rotation=False, zoom=0.1, validation_split=.15, seed=7, do_ahisteq=False)\n", + "valid_generator = SegClass.create_generators(blur=0, crop_shape=None, mode='validation', \n", + " n_classes=n_classes, horizontal_flip=True, vertical_flip=False, \n", + " brightness=.1, rotation=False, zoom=.05, validation_split=.15, \n", + " seed=7, do_ahisteq=False)" ] }, { @@ -120,13 +145,14 @@ "mode = 'max'\n", "\n", "# fine-tune model (train only last conv layers)\n", - "flag = 0\n", - " for k, l in enumerate(model.layers):\n", - " l.trainable = False\n", - " if l.name == 'concat_projection':\n", - " flag = 1\n", - " if flag:\n", - " l.trainable = True\n", + "if load_pretrained_weights:\n", + " flag = 0\n", + " for k, l in enumerate(model.layers):\n", + " l.trainable = False\n", + " if l.name == 'concat_projection':\n", + " flag = 1\n", + " if flag:\n", + " l.trainable = True\n", " \n", "def build_callbacks(tf_board = False):\n", " tensorboard = TensorBoard(log_dir='./logs/'+SegClass.net, histogram_freq=0,\n", @@ -341,7 +367,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.2" + "version": "3.6.8" } }, "nbformat": 4, diff --git a/utils.py b/utils.py index 7774f75f..0ca995ac 100644 --- a/utils.py +++ b/utils.py @@ -152,10 +152,10 @@ def create_seg_model(self, net, n=21, backbone='mobilenetv2', load_weights=False 2. original deeplab v3+ and subpixel upsampling layer ''' - model = Deeplabv3(weights=None, input_tensor=None, infer = False, - input_shape = self.sz + (3,), classes=21, + model = Deeplabv3(weights=None, input_tensor=None, infer=False, + input_shape=self.sz + (3,), classes=21, backbone=backbone, OS=16, alpha=1) - + base_model = Model(model.input, model.layers[-5].output) self.net = net self.modelpath = 'weights/{}_{}.h5'.format(backbone, net) @@ -191,9 +191,9 @@ def create_seg_model(self, net, n=21, backbone='mobilenetv2', load_weights=False self.model = model return model - def create_generators(self, crop_shape = False, mode = 'train', do_ahisteq = True, - n_classes = 21, horizontal_flip = True, vertical_flip = False, blur = False, with_bg = True, - brightness=0.1, rotation=5.0, zoom=0.1, validation_split = .2, seed = 7): + def create_generators(self, crop_shape=False, mode='train', do_ahisteq=True, n_classes=21, horizontal_flip=True, + vertical_flip=False, blur=False, with_bg=True, brightness=0.1, rotation=5.0, + zoom=0.1, validation_split=.2, seed=7): generator = SegmentationGenerator(folder = self.mainpath, mode = mode, n_classes = n_classes, do_ahisteq = do_ahisteq, batch_size=self.batch_size, resize_shape=self.sz[::-1], crop_shape=crop_shape,