Ejemplo n.º 1
0
    def test_unet_model(self):
        image = Raster \
            .read \
            .format("geotiff") \
            .load(TEST_IMAGE_PATH)

        label: Raster = Raster \
            .read \
            .format("shp") \
            .options(
            pixel=image.pixel,
            extent=image.extent
        ) \
            .load(self.shape_path)

        standarize1 = ImageStand(raster=image)
        standarized = standarize1.standarize_image(StandardScaler())
        raster_data = RasterData(standarized, label)
        unet_images = raster_data.prepare_unet_data(image_size=[64, 64])

        callbacks = [
            EarlyStopping(patience=100, verbose=1),
            ReduceLROnPlateau(factor=0.1, patience=100, min_lr=0, verbose=1),
            ModelCheckpoint('model_more_class_pixels.h5',
                            verbose=1,
                            save_best_only=True,
                            save_weights_only=False)
        ]
        config = UnetConfig(
            input_size=[64, 64, 3],
            metrics=["accuracy"],
            optimizer=SGD(lr=0.001),
            callbacks=callbacks,
            loss="binary_crossentropy",
        )
        #
        unet = Unet(config=config)
        unet.compile()
        unet.fit(unet_images, epochs=1)
        predicted = unet.predict(x=unet_images.x_test[0], threshold=0.4)
        SubPlots().extend(predicted, unet_images.x_test[0]).plot(nrows=1)
Ejemplo n.º 2
0
tfconfig = tf.ConfigProto()
tfconfig.gpu_options.allow_growth = True
tfconfig.allow_soft_placement = True
sess = tf.Session(config=tfconfig)
sess.run(tf.compat.v1.global_variables_initializer())
keras.backend.set_session(sess)

input_img = Input((512, 512, 3))
model = Unet(input_img, 16, 0.1, True)
learning_rate = 0.001
epochs = 500
decay_rate = learning_rate / epochs
model.compile(optimizer=Adam(lr=learning_rate, decay=decay_rate), loss='mse')
model.summary()

history = model.fit(x=x, y=x, batch_size=32, epochs=20)

maxpool_model = keras.Sequential()
maxpool_model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=2))

flair_encoder = Model(model.input, model.get_layer('activation_10').output)
flair_encoder.summary()

bottleneck = flair_encoder.predict(x)
bottleneck = maxpool_model.predict(bottleneck)
print('Shape of bottleneck: ', bottleneck.shape)
bottleneck = bottleneck.reshape((409, 65536))

y = to_categorical(y, num_classes=8, dtype="uint8")

x_train, x_val, y_train, y_val = train_test_split(bottleneck, y, test_size=0.2)
Ejemplo n.º 3
0
            # imgplot = plt.imshow(Y)
            # plt.show(block=False)
            # plt.pause(3)
            # plt.close()

    #hello = y_to.flatten()
    #print(hello[hello==3].shape)
    #print("Number of classes",np.unique(hello))
    #class_weights = class_weight.compute_class_weight('balanced',np.unique(hello),hello)

    #class_weights.insert(3,0)
    #print("class_weights",class_weights)
x_to = np.asarray(x_to)
y_to = np.asarray(y_to)
print(x_to.shape)
print(y_to.shape)

y_to[
    y_to ==
    4] = 1  #since label 4 was missing in Brats dataset , changing all labels 4 to 3.
#y_to = one_hot_encode(y_to)
y_to[y_to == 2] = 1
y_to[y_to == 1] = 1
y_to[y_to == 0] = 0
print(y_to.shape)
#y_to = y_to.reshape(240,240,1)

model.fit(x=x_to, y=y_to, batch_size=20, epochs=50)

model.save('2class.h5')
    for j in range(100):
        flair_small[i, :, :, j] = flair_small_[i, :, :, j+36]

print('Shape of flair_small: ' + str(flair_small.shape))


os.environ["CUDA_VISIBLE_DEVICES"] = "0, 1, 2, 3"

tfconfig = tf.ConfigProto()
tfconfig.gpu_options.allow_growth = True
tfconfig.allow_soft_placement = True
sess = tf.Session(config=tfconfig)
sess.run(tf.global_variables_initializer())
keras.backend.set_session(sess)

# history = model.fit(x=X_train_t1, y=X_train_t1, batch_size=32, epochs=100)
# history = model.fit(x=X_train_t1ce, y=X_train_t1ce, batch_size=32, epochs=100)
# history = model.fit(x=X_train_t2, y=X_train_t2, batch_size=32, epochs=100)
history = model.fit(x=flair_small, y=flair_small, batch_size=32, epochs=100)

print(history.history.keys())

plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train'], loc='upper right')
plt.show()

model.save("flair_model")