Ejemplo n.º 1
0
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, dsize=(image_w, image_h))
        result = model.predict(image.reshape(1, image_h, image_w, 3))
        res = result[0, :, :, 0]
        # if image sise different from 240x320
        mask = cv2.resize(res, dsize=(240, 320))
        kernel = np.ones((5, 5), np.float32) / 25
        mask = cv2.blur(mask, (5, 5))
        # mask = cv2.GaussianBlur(mask,(3,3),0)
        mask[mask > 0.5] = 1
        mask[mask <= 0.5] = 0
        # cv2.imshow('image', mask)
        # cv2.waitKey(0)
        # exit()
        mask = np.asarray(mask, bool)
        res = remove_small_objects(mask, min_size=256, connectivity=1)
        res = np.asarray(res, 'uint8')
        enc = rle_encode(res)
        print('Saving ' + str(i) + ' image..')
        sample.set_value(int(img.split('.')[0]), 'rle_mask', enc)

    sample.to_csv(exargs.name + '.csv')

    model_json = model.to_json()
    json_file = open('../models/' + exargs.name + '.json', 'w')
    json_file.write(model_json)
    json_file.close()
    print('Model saved!')

    K.clear_session()
Ejemplo n.º 2
0
m.compile(optimizer=opt, loss='categorical_crossentropy', metrics=[iou_score])

# fit model
weights_path = args.ckpt_path + 'weights.{epoch:02d}-{val_loss:.2f}-{val_iou_score:.2f}.hdf5'
callbacks = get_callbacks(weights_path, args.ckpt_path, 5, args.opt)

history = m.fit_generator(train_gen,
                          epochs=args.epochs,
                          steps_per_epoch=(NO_OF_TRAINING_IMAGES //
                                           BATCH_SIZE),
                          validation_data=(val_x / 255, val_y),
                          shuffle=True,
                          callbacks=callbacks)
''' save model structure '''
model_json = m.to_json()
with open(os.path.join(args.ckpt_path, "model.json"), "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
print("Saved model to disk")
m.save(os.path.join(args.ckpt_path, 'model.h5'))
'''Evaluate and Test '''
print('======Start Evaluating======')
#don't use generator but directly from array
score = m.evaluate(val_x / 255, val_y, verbose=0)
print("%s: %.2f%%" % (m.metrics_names[0], score[0] * 100))
print("%s: %.2f%%" % (m.metrics_names[1], score[1] * 100))
with open(os.path.join(args.ckpt_path, 'output.txt'), "w") as file:
    file.write("%s: %.2f%%" % (m.metrics_names[0], score[0] * 100))
    file.write("%s: %.2f%%" % (m.metrics_names[1], score[1] * 100))
Ejemplo n.º 3
0
    def workflow(self):
        # define model
        model = Unet(backbone_name='resnet50', encoder_weights='imagenet')
        adam = keras.optimizers.Adam(lr=self.cfgs["LEARNING_RATE"])
        model.summary()
        # model.compile('Adam', sigmoid_cross_entropy_balanced)
        model.compile(
            'Adam',
            # cross_entropy_balanced
            loss=self.define_loss(),
            # 'binary_crossentropy'
        )

        test_images, test_ulabels, test_elabels, test_rlabels, filelist = self.dl.get_test_data(
        )

        if self.cfgs["RESTORE"]:
            model.load_weights(
                os.path.join(self.cfgs["SAVE_DIR"], "weights", "epoch150.h5"))
            print("RETORE SUCCESSFULLY!")

        callback = TensorBoard('./graph')
        callback.set_model(model)
        train_names = [
            'loss', 'u_outputs_sig_loss', 'e_fuse_sig_loss', 'r_fuse_sig_loss',
            'fuse_dir_loss'
        ]

        current_learning_rate = self.cfgs["LEARNING_RATE"]
        K.set_value(model.optimizer.lr, current_learning_rate)
        for i in range(self.cfgs["EPOCH"]):
            print("[I] EPOCH {}".format(i))
            # TRAIN
            for j in tqdm(range(self.cfgs["STEP"])):
                images_batch, ulabels_batch, elabels_batch, rlabels_batch, d_labels_batch = self.dl.next_batch(
                    "train")
                Logs = model.train_on_batch(
                    images_batch,
                    self.define_train_y(ulabels_batch, elabels_batch,
                                        rlabels_batch, d_labels_batch),
                )

            write_log(callback, train_names, Logs, i)
            if i % self.cfgs["INTERVAL"] == 0 and i >= 0:

                # TEST:
                results = model.predict(test_images, batch_size=10, verbose=0)
                logits = results[-1]
                r_logits = results[-2]

                # result analyse and show
                rlt_worker = ResultManager(i, logits, test_ulabels)
                # r_analyst.compute_roc(savename='roc_vegas_{}.csv'.format(i))
                # rlt_worker_r = ResultManager(i, r_logits, test_rlabels)

                rlt_worker.run()
                # rlt_worker_r.run()

                for ii in range(results[0].shape[0]):
                    #                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/images/{}'.format(filelist[ii][0])),
                    #                                 test_images[ii, :] * 255)
                    #                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/labels/{}'.format(filelist[ii][1])),
                    #                                 test_ulabels[ii, :] * 255)
                    #                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/labels_e/{}'.format(filelist[ii][1])),
                    #                                 test_elabels[ii, :] * 255)
                    #                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/labels_r/{}'.format(filelist[ii][1])),
                    #                                 test_rlabels[ii, :] * 255)

                    #cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/preds/{}'.format(filelist[ii][1])),
                    #           results[-1][ii, :])
                    pred_threshold = threshold(results[-1][ii, :])
                    cv2.imwrite(
                        os.path.join(
                            self.cfgs["SAVE_DIR"],
                            'main_outputs/preds_threshold/{}'.format(
                                filelist[ii][1])), pred_threshold * 255)


#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e1/{}'.format(filelist[ii][1])),
#                                 threshold(results[1][ii, :]) * 255)
#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e2/{}'.format(filelist[ii][1])),
#                                 threshold(results[2][ii, :]) * 255)
#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e3/{}'.format(filelist[ii][1])),
#                                 threshold(results[3][ii, :]) * 255)
#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e4/{}'.format(filelist[ii][1])),
#                                 threshold(results[4][ii, :]) * 255)
#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e5/{}'.format(filelist[ii][1])),
#                                 threshold(results[5][ii, :]) * 255)
#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r1/{}'.format(filelist[ii][1])),
#                                 threshold(results[6][ii, :]) * 255)
#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r2/{}'.format(filelist[ii][1])),
#                                 threshold(results[7][ii, :]) * 255)
#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r3/{}'.format(filelist[ii][1])),
#                                 threshold(results[8][ii, :]) * 255)
#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r4/{}'.format(filelist[ii][1])),
#                                 threshold(results[9][ii, :]) * 255)
#                     cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r5/{}'.format(filelist[ii][1])),
#                                 threshold(results[10][ii, :]) * 255)

# SAVE WEIGHTS
                current_learning_rate = current_learning_rate * self.cfgs[
                    "LEARNING_RATE_DECAY"]
                K.set_value(model.optimizer.lr, current_learning_rate)
                print('[I] Current Learning Rate: ', current_learning_rate)
                model_json = model.to_json()
                with open("model.json", "w") as json_file:
                    json_file.write(model_json)
                model.save_weights(
                    os.path.join(self.cfgs["SAVE_DIR"],
                                 "epoch{}.h5".format(i)))