def diff_model(self):
        with tf.device('cpu'):
            ds = get_tr_ds(
                original_pattern='/opt/dataset/tr2_cropped/data/*.png',
                mask_pattern='/opt/dataset/tr2_cropped/label/*.png',
                batch_size=2)

            def reshape_fn(d, l):
                d = tf.cast(tf.reshape(d, (-1, 1024, 1024, 3)),
                            tf.float32) / 255.0
                l = tf.reshape(l, (-1, 1024, 1024, 3))
                return d, l

            ds = ds.map(reshape_fn)

        model = Deeplab(num_classes=3)
        # model.trainable = True
        model.compile(
            optimizer=tf.keras.optimizers.Adam(),
            loss=tf.keras.losses.BinaryCrossentropy(),
            # loss=FocalLoss()
        )
        #   metrics=[tf.keras.metrics.MeanIoU(num_classes=3)])
        routine = TrainRoutine(ds=ds, model=model)
        routine.run(exp_dir='exp/01', epochs=1)

        self.assertTrue(os.path.exists('exp/01/config/exp_config.json'))
Ejemplo n.º 2
0
def train(loss_list,
          freeze_expdir_list,
          unfreeze_expdir_list,
          batch_size=32,
          resize=True,
          freeze_epochs=10,
          unfreeze_epochs=5):
    with tf.device('cpu'):
        ds = get_tr_ds(original_pattern='/opt/dataset/tr3_cropped/data/*.png',
                       mask_pattern='/opt/dataset/tr3_cropped/label/*.png',
                       batch_size=batch_size)

        def reshape_fn(d, l):
            d = tf.cast(tf.reshape(d, (-1, 1024, 1024, 3)), tf.float32)
            l = l[:, :, :, 1]
            l = tf.one_hot(l, 4)
            l = tf.reshape(l, (-1, 1024, 1024, 4))
            if resize:
                l = tf.image.resize(l, [224, 224])
            return d, l

        ds = ds.map(reshape_fn)

    for i in range(len(loss_list)):
        model = Deeplab(num_classes=4)
        model.layers[0].trainable = False
        model.compile(optimizer=tf.keras.optimizers.Adam(),
                      loss=loss_list[i],
                      metrics=[MeanIou(num_classes=4)])
        routine = TrainRoutine(ds=ds, model=model)
        routine.run(exp_dir=freeze_expdir_list[i], epochs=freeze_epochs)

        model.layers[0].trainable = True
        model.compile(optimizer=tf.keras.optimizers.Adam(),
                      loss=loss_list[i],
                      metrics=[MeanIou(num_classes=4)])
        routine = TrainRoutine(ds=ds, model=model)
        routine.run(exp_dir=unfreeze_expdir_list[i], epochs=unfreeze_epochs)
Ejemplo n.º 3
0
def apply_clustering_to_conv2D(layer):
    if isinstance(layer, tf.keras.layers.Conv2D):
        return cluster_weights(layer, **clustering_params)
    return layer


clustered_model = tf.keras.models.clone_model(
    base_model,
    clone_function=apply_clustering_to_conv2D,
)

clustered_model.summary()

with tf.device('/cpu:0'):
    ds = get_tr_ds(original_pattern='/opt/dataset/tr2_cropped/data/*.png',
                   mask_pattern='/opt/dataset/tr2_cropped/label/*.png',
                   batch_size=1)

    def reshape_fn(d, l):
        d = tf.cast(tf.reshape(d, (-1, 1024, 1024, 3)), tf.float32) / 255.0
        l = l[:, :, :, 1]
        l = tf.one_hot(l, 4)
        l = tf.reshape(l, (-1, 1024, 1024, 4))
        return d, l

    ds = ds.map(reshape_fn)

clustered_model.compile(optimizer=tf.keras.optimizers.Adam(),
                        loss=LovaszLoss(),
                        metrics=[MeanIou(num_classes=4)])
clustered_model.fit(ds, epochs=1)
Ejemplo n.º 4
0
 def get_tr_ds_demo(self):
     ds = get_tr_ds(original_pattern='/opt/dataset/tr2_cropped/data/*.png',
                    mask_pattern='/opt/dataset/tr2_cropped/label/*.png')
     for d in ds.take(1):
         self.assertEqual(np.array(d).shape[1:], (2, 1024, 1024, 3))