def batch_generator_val(files, image_classes, batch_size, data_path=None):
    nn_shape = 299
    threads = 8

    # threads = 1
    p = ThreadPool(threads)
    process_item_func = partial(process_single_item_npz, data_path=data_path, box_size=nn_shape, name_ext='.npz')
    index = np.arange(0, files.shape[0])
    np.random.shuffle(index)

    start_index = 0
    while True:
        batch_index = index[start_index:start_index + batch_size]

        start_index += batch_size
        if start_index > len(files) - batch_size:
            start_index = 0

        batch_files = files[batch_index].copy()
        batch_target = get_target(batch_files, image_classes)
        batch_images = p.map(process_item_func, batch_files)
        batch_images = np.array(batch_images, np.float32)
        if 0:
            print(batch_images.shape, batch_target.shape)
            show_image(batch_images[0])
            print(batch_target[0], batch_target[0].sum())

        batch_images = xception_preprocess_input(batch_images)
        yield batch_images, batch_target
Example #2
0
def val_pre_model(source_path, folder, img_dim, architechture):

    array_path = os.path.join(source_path, folder)
    pre_model_path = os.path.join(source_path, 'pre_model')
    shutil.rmtree(pre_model_path,ignore_errors=True)
    os.makedirs(pre_model_path)

    if architechture == 'resnet50':
        popped, pre_model = get_resnet_pre_model(img_dim)
    elif architechture == 'xception':
        popped, pre_model = get_xception_pre_model(img_dim)
    else:
        popped, pre_model = get_inception_v3_pre_model(img_dim)

    for (array, label, array_name, label_name) in tqdm(gen_array_from_dir(array_path)):
        if architechture == 'resnet50':
            array = resnet_preprocess_input(array[np.newaxis].astype(np.float32))
        elif architechture == 'xception':
            array = xception_preprocess_input(array[np.newaxis].astype(np.float32))
        else:
            array = inception_v3_preprocess_input(array[np.newaxis].astype(np.float32))
        array_pre_model = np.squeeze(pre_model.predict(array, batch_size=1))

        array_name = array_name.split('.')[0]
        label_name = label_name.split('.')[0]

        img_pre_model_path = os.path.join(pre_model_path, array_name)
        label_pre_model_path = os.path.join(pre_model_path, label_name)

        np.save(img_pre_model_path, array_pre_model)
        np.save(label_pre_model_path, label)
Example #3
0
def val_pre_model(source_path, folder, img_dim, architechture):

    array_path = os.path.join(source_path, folder)
    pre_model_path = os.path.join(source_path, 'pre_model')
#    call(['rm', '-rf', pre_model_path])
    shutil.rmtree(pre_model_path,ignore_errors=True)
#    call(['mkdir', '-p', pre_model_path])
    os.makedirs(pre_model_path)

    if architechture == 'resnet50':
        popped, pre_model = get_resnet_pre_model(img_dim)
    elif architechture == 'xception':
        popped, pre_model = get_xception_pre_model(img_dim)
    else:
        popped, pre_model = get_inception_v3_pre_model(img_dim)

    for (array, label, array_name, label_name) in tqdm(gen_array_from_dir(array_path)):
        if architechture == 'resnet50':
            array = resnet_preprocess_input(array[np.newaxis].astype(np.float32))
        elif architechture == 'xception':
            array = xception_preprocess_input(array[np.newaxis].astype(np.float32))
        else:
            array = inception_v3_preprocess_input(array[np.newaxis].astype(np.float32))
        array_pre_model = np.squeeze(pre_model.predict(array, batch_size=1))

        array_name = array_name.split('.')[0]
        label_name = label_name.split('.')[0]

        img_pre_model_path = os.path.join(pre_model_path, array_name)
        label_pre_model_path = os.path.join(pre_model_path, label_name)

        np.save(img_pre_model_path, array_pre_model)
        np.save(label_pre_model_path, label)
Example #4
0
def gen_minibatches(array_dir, array_names, batch_size, architecture, final = False):

    array_names = list(array_names)

    while True:
        # in place shuffle
        np.random.shuffle(array_names)
        array_names_mb = array_names[:batch_size]

        arrays = []
        labels = []
        for array_name in array_names_mb:
            img_path = os.path.join(array_dir, array_name)
            array = np.load(img_path)
            if final:
                if architecture == 'resnet50':
                    array = np.squeeze(resnet_preprocess_input(array[np.newaxis].astype(np.float32)))
                elif architecture == 'xception':
                    array = np.squeeze(xception_preprocess_input(array[np.newaxis].astype(np.float32)))
                else:
                    array = np.squeeze(inception_v3_preprocess_input(array[np.newaxis].astype(np.float32)))

            arrays.append(array)
            labels.append(np.load(img_path.replace('-img-','-label-')))

        yield np.array(arrays), np.array(labels)
Example #5
0
def preprocess_input(image, model_type):
    if model_type == "vgg19":
        return vgg19_preprocess_input(image)
    if model_type == "resnet50":
        return resnet50_preprocess_input(image)
    if model_type == "xception":
        return xception_preprocess_input(image)

    print(f"Using fallback for model type {model_type}")
    return image
def create_submission(model_path, is_weights=False):
    from keras.models import load_model
    from keras.utils import multi_gpu_model
    from keras.optimizers import Adam, SGD

    print('\nModel weights: ', model_path, '\n')
    if is_weights:

        optim_type = 'Adam'
        learning_rate = 0.001

        ##### Prepare the model #####
        model = get_model_xception()
        if optim_type == 'SGD':
            optim = SGD(lr=learning_rate,
                        decay=1e-6,
                        momentum=0.9,
                        nesterov=True)
        else:
            optim = Adam(lr=learning_rate)
        model = multi_gpu_model(model, gpus=2)
        model.compile(optimizer=optim,
                      loss='binary_crossentropy',
                      metrics=[f2beta_loss, fbeta])

        model.load_weights(model_path)
    else:
        model = load_model(model_path,
                           custom_objects={
                               'f2beta_loss': f2beta_loss,
                               'fbeta': fbeta
                           })
    test_files = glob.glob(TUNING_IMAGE_PATH)
    print('Test valid:', len(test_files))

    for i, f in enumerate(sorted(test_files)):
        if i % 500 == 0:
            print(i)
        id = os.path.basename(f)[:-4]
        path = f

        cache_path = CACHE_PATH_TEST + id + FILE_EXT
        if not os.path.isfile(cache_path):
            im_full_big = load_img_npz(path)

            batch_images = []
            batch_images.append(im_full_big.copy())
            batch_images.append(im_full_big[:, ::-1, :].copy())
            batch_images = np.array(batch_images, dtype=np.float32)
            batch_images = xception_preprocess_input(batch_images)

            preds = model.predict(batch_images)
            preds[preds < EPS] = 0
            preds = np.round(preds, PRECISION)
            save_in_file(preds, cache_path)
Example #7
0
def multi_predict(aug_gen, models, architecture):
    predicted = []
    for img, _ in aug_gen:
        if architecture == 'resnet50':
            img = resnet_preprocess_input(img[np.newaxis].astype(np.float32))
        elif architecture == 'xception':
            img = xception_preprocess_input(img[np.newaxis].astype(np.float32))
        else:
            img = inception_v3_preprocess_input(img[np.newaxis].astype(np.float32))
        for model in models:
            predicted.append(model.predict(img))
    predicted = np.array(predicted).sum(axis=0)
    pred_list = list(predicted[0])
    return predicted, pred_list
Example #8
0
def multi_predict(aug_gen, models, architecture):
    predicted = []
    for img, _ in aug_gen:
        if architecture == 'resnet50':
            img = resnet_preprocess_input(img[np.newaxis].astype(np.float32))
        elif architecture == 'xception':
            img = xception_preprocess_input(img[np.newaxis].astype(np.float32))
        else:
            img = inception_v3_preprocess_input(img[np.newaxis].astype(np.float32))
        for model in models:
            predicted.append(model.predict(img))
    predicted = np.array(predicted).sum(axis=0)
    pred_list = list(predicted[0])
    return predicted, pred_list
Example #9
0
def visualize_image(img_name):
    image_size = (224, 224)
    original_image = image.load_img(img_name, target_size=image_size)
    img = image.img_to_array(original_image)
    img = np.expand_dims(img, axis=0)
    img = xception_preprocess_input(img)
    global sess
    global graph
    with graph.as_default():
        set_session(sess)
        vis = visualization([img])[0][0]
        disease = ResTS.predict(img)[0]
        probab = max(disease[0])
        disease = np.argmax(disease)
        heatmap = postprocess_vis(vis)
    img = plt.imshow(heatmap, cmap='Reds')
    plt.axis('off')
    plt.savefig(img_name, bbox_inches='tight')
    return disease, probab
def get_tuning_labels_data_for_validation_weimin(box_size=336):
    from a01_neural_nets import preprocess_input
    cache_path = OUTPUT_PATH + 'tuning_labels_data_{}.pklz'.format(box_size)
    if not os.path.isfile(cache_path):
        s1 = pd.read_csv(INPUT_PATH + 'tuning_labels.csv')
        test_image_classes = get_classes_for_tst_images_dict()
        ids = list(s1['image_id'].values)
        images = []
        images_target = get_target_v2(ids, test_image_classes).astype(np.uint8)
        for i, id in enumerate(ids):
            path = INPUT_PATH + 'stage_1_test_images/' + id + '.jpg'
            img = read_single_image(path)
            img = cv2.resize(img, (box_size, box_size), cv2.INTER_LANCZOS4)
            images.append(img)
        images = np.array(images, dtype=np.float32)
        images = xception_preprocess_input(images)
        save_in_file((images, images_target), cache_path)
    else:
        images, images_target = load_from_file(cache_path)

    return images, images_target
Example #11
0
def train_model(project, final=False, last=False):
    weight_label = '-' + project['architecture'] + '-weights-'
    source_path = project['path']
    weights_path = os.path.join(source_path, 'weights')
    plot_path = os.path.join(source_path, 'plots')
    if last:
        weights = 'last_weights'
    else:
        weights = 'best_weights'

    if final:
        weight_label += '-final-'
        use_path = os.path.join(source_path, 'augmented')
    else:
        use_path = os.path.join(source_path, 'pre_model')

    project['model_round'] += 1
    shutil.rmtree(weights_path, ignore_errors=True)
    os.makedirs(weights_path)
    shutil.rmtree(plot_path, ignore_errors=True)
    os.makedirs(plot_path)

    img_dim = project['img_dim'] * project['img_size']
    conv_dim = project['conv_dim'] * project['img_size']

    lr = project['learning_rate']
    decay = project['learning_rate_decay']

    all_files = os.listdir(use_path)
    pre_model_files = list(filter(lambda x: r'-img-' in x, all_files))
    label_names = list(filter(lambda x: r'-label-' in x, all_files))

    pre_model_files_df = pd.DataFrame({'files': pre_model_files})
    pre_model_files_df['suffix'] = pre_model_files_df.apply(
        lambda row: row.files.split('.')[-1], axis=1)
    pre_model_files_df = pre_model_files_df[pre_model_files_df.suffix == 'npy']
    pre_model_files_df['ind'] = pre_model_files_df.apply(
        lambda row: row.files.split('-')[0], axis=1).astype(int)
    pre_model_files_df['label'] = pre_model_files_df.apply(
        lambda row: row.files.split('-')[3], axis=1)

    pre_model_files_df_dedup = pre_model_files_df.drop_duplicates(subset='ind')
    pre_model_files_df = pre_model_files_df.set_index(['ind'])

    pre_model_files.sort()
    label_names.sort()

    pre_model_files_arr = np.array(pre_model_files)
    label_names_arr = np.array(label_names)

    labels = [
        np.argmax(np.load(os.path.join(use_path, label_name)))
        for label_name in label_names
    ]
    best_weights = []
    last_weights = []

    if project['kfold'] >= 3:
        kfold = StratifiedKFold(n_splits=project['kfold'],
                                shuffle=True,
                                random_state=project['seed'])
        kfold_generator = kfold.split(pre_model_files_df_dedup,
                                      pre_model_files_df_dedup.label)
        validate = True
    else:
        print('Too few k-folds selected, fitting on all data')
        kfold_generator = no_folds_generator(pre_model_files_df_dedup)
        validate = False

    for i, (train, test) in enumerate(kfold_generator):
        if project['kfold_every']:
            print('----- Fitting Fold', i, '-----')
        elif i > 0:
            break

        weights_name = project['name'] + weight_label + '-kfold-' + str(
            i) + '-round-' + str(project['model_round']) + '.hdf5'
        plot_name = project['name'] + weight_label + '-kfold-' + str(
            i) + '-round-' + str(project['model_round']) + '.png'

        if project[weights] is None:
            fold_weights = None
        else:
            fold_weights = project[weights][i]
        if final:
            if project['architecture'] == 'resnet50':
                model = get_resnet_final_model(img_dim, conv_dim,
                                               project['number_categories'],
                                               fold_weights,
                                               project['is_final'])
            elif project['architecture'] == 'xception':
                model = get_xception_final_model(img_dim, conv_dim,
                                                 project['number_categories'],
                                                 fold_weights,
                                                 project['is_final'])
            else:
                model = get_inception_v3_final_model(
                    img_dim, conv_dim, project['number_categories'],
                    fold_weights, project['is_final'])

            for i, layer in enumerate(model.layers[1].layers):
                if len(layer.trainable_weights) > 0:
                    if i < project['final_cutoff']:
                        mult = 0.01
                    else:
                        mult = 0.1
                    layer.learning_rate_multiplier = [
                        mult for tw in layer.trainable_weights
                    ]

        else:
            if project['architecture'] == 'resnet50':
                pre_model, model = get_resnet_pre_post_model(
                    img_dim,
                    conv_dim,
                    len(project['categories']),
                    model_weights=fold_weights)
            elif project['architecture'] == 'xception':
                pre_model, model = get_xception_pre_post_model(
                    img_dim,
                    conv_dim,
                    len(project['categories']),
                    model_weights=fold_weights)
            else:
                pre_model, model = get_inception_v3_pre_post_model(
                    img_dim,
                    conv_dim,
                    len(project['categories']),
                    model_weights=fold_weights)

        pre_model_files_dedup_train = pre_model_files_df_dedup.iloc[train]
        train_ind = list(set(pre_model_files_dedup_train.ind))
        pre_model_files_train = pre_model_files_df.loc[train_ind]

        gen_train = gen_minibatches(use_path,
                                    pre_model_files_train.files,
                                    project['batch_size'],
                                    project['architecture'],
                                    final=final)
        number_train_samples = len(pre_model_files_train)

        if validate:
            pre_model_files_dedup_test = pre_model_files_df_dedup.iloc[test]
            test_ind = list(set(pre_model_files_dedup_test.ind))
            pre_model_files_test = pre_model_files_df.loc[test_ind]

            gen_test = gen_minibatches(use_path,
                                       pre_model_files_test.files,
                                       project['batch_size'],
                                       project['architecture'],
                                       final=final)
            number_test_samples = len(pre_model_files_test)
            validation_steps = (number_test_samples // project['batch_size'])

            weights_checkpoint_file = weights_name.split(
                '.'
            )[0] + '-kfold-' + str(
                i
            ) + "-improvement-{epoch:02d}-{val_categorical_accuracy:.4f}.hdf5"
            checkpoint = ModelCheckpoint(os.path.join(weights_path,
                                                      weights_checkpoint_file),
                                         monitor='val_categorical_accuracy',
                                         verbose=1,
                                         save_best_only=True,
                                         mode='max')

            callbacks_list = [checkpoint]
        else:
            gen_test = None
            validation_steps = None
            callbacks_list = None

        steps_per_epoch = (number_train_samples // project['batch_size'])
        for j in range(project['rounds']):
            optimizer = Adam(lr=lr, decay=decay)

            model.compile(optimizer=optimizer,
                          loss='categorical_crossentropy',
                          metrics=['categorical_accuracy'])

            model.fit_generator(gen_train,
                                steps_per_epoch=steps_per_epoch,
                                epochs=project['cycle'] * (j + 1),
                                verbose=1,
                                validation_data=gen_test,
                                validation_steps=validation_steps,
                                initial_epoch=j * project['cycle'],
                                callbacks=callbacks_list)

        model.save_weights(os.path.join(weights_path, weights_name))
        last_weights.append(os.path.join(weights_path, weights_name))
        weights_names = os.listdir(weights_path)
        max_val = -1
        max_i = -1
        for j, name in enumerate(weights_names):
            if name.find(weights_name.split('.')[0]) >= 0:
                if (name.find(weight_label) >= 0) and (name.find('improvement')
                                                       >= 0):
                    val = int(name.split('.')[1])
                    if val > max_val:
                        max_val = val
                        max_i = j
        if project['plot']:
            print('Plotting confusion matrix')

            if max_i == -1:
                print('Loading last weights:',
                      os.path.join(weights_path, weights_name))
                model.load_weights(os.path.join(weights_path, weights_name))
            else:
                print('Loading best weights:',
                      os.path.join(weights_path, weights_names[max_i]))
                model.load_weights(
                    os.path.join(weights_path, weights_names[max_i]))
            best_predictions = []
            true_labels = []

            print('Predicting test files')
            if validate:
                use_files = pre_model_files_test.files
            else:
                use_files = pre_model_files_train.files
            for array_name in tqdm(use_files):
                img_path = os.path.join(use_path, array_name)
                img = np.load(img_path)
                if final:
                    if project['architecture'] == 'resnet50':
                        img = np.squeeze(
                            resnet_preprocess_input(img[np.newaxis].astype(
                                np.float32)))
                    elif project['architecture'] == 'xception':
                        img = np.squeeze(
                            xception_preprocess_input(img[np.newaxis].astype(
                                np.float32)))
                    else:
                        img = np.squeeze(
                            inception_v3_preprocess_input(
                                img[np.newaxis].astype(np.float32)))
                prediction = model.predict(img[np.newaxis])
                best_predictions.append(
                    project['categories'][np.argmax(prediction)])
                true_label = np.load(img_path.replace('-img-', '-label-'))
                true_labels.append(
                    project['categories'][np.argmax(true_label)])

            cm = confusion_matrix(true_labels, best_predictions,
                                  project['categories'])
            plt.clf()
            sns.heatmap(pd.DataFrame(cm, project['categories'],
                                     project['categories']),
                        annot=True,
                        fmt='g')
            plt.xlabel('Actual')
            plt.xlabel('Predicted')
            plt.xticks(rotation=45, fontsize=8)
            plt.yticks(rotation=45, fontsize=8)
            plt.title('Confusion matrix for fold: ' + str(i) + '\nweights' +
                      weights_name)
            plt.savefig(os.path.join(plot_path, plot_name))
            print('Confusion matrix plot saved:',
                  colored(os.path.join(plot_path, plot_name), 'magenta'))

        if max_i == -1:
            best_weights.append(os.path.join(weights_path, weights_name))
        else:
            best_weights.append(
                os.path.join(weights_path, weights_names[max_i]))

    project['number_categories'] = len(project['categories'])
    project['best_weights'] = best_weights
    project['last_weights'] = last_weights
    project['is_final'] = final

    return project
 def predict_breed(self, img_path):
     bottleneck_feature = self.xception.predict(
         xception_preprocess_input(self.path_to_tensor(img_path)))
     predicted_vector = self.model.predict(bottleneck_feature)
     return dog_names[np.argmax(predicted_vector)]
Example #13
0
def extract_Xception(tensor):
	from keras.applications.xception import Xception
	from keras.applications.xception import preprocess_input as xception_preprocess_input
	return Xception(weights='imagenet', include_top=False).predict(xception_preprocess_input(tensor))
Example #14
0
def train_model(project, final = False, last = False):
    weight_label = '-' + project['architecture'] + '-weights-'
    source_path = project['path']
    weights_path = os.path.join(source_path, 'weights')
    plot_path = os.path.join(source_path, 'plots')
    if last:
        weights = 'last_weights'
    else:
        weights = 'best_weights'

    if final:
        weight_label += '-final-'
        use_path = os.path.join(source_path, 'augmented')
    else:
        use_path = os.path.join(source_path, 'pre_model')

    project['model_round'] += 1
    shutil.rmtree(weights_path,ignore_errors=True)
    os.makedirs(plot_path)

    img_dim = project['img_dim'] * project['img_size']
    conv_dim = project['conv_dim'] * project['img_size']

    lr =  project['learning_rate']
    decay = project['learning_rate_decay']

    all_files = os.listdir(use_path)
    pre_model_files = list(filter(lambda x: r'-img-' in x, all_files))
    label_names = list(filter(lambda x: r'-label-' in x, all_files))

    pre_model_files_df = pd.DataFrame({'files': pre_model_files})
    pre_model_files_df['suffix'] = pre_model_files_df.apply(lambda row: row.files.split('.')[-1], axis = 1)
    pre_model_files_df = pre_model_files_df[pre_model_files_df.suffix == 'npy']
    pre_model_files_df['ind'] = pre_model_files_df.apply(lambda row: row.files.split('-')[0], axis = 1).astype(int)
    pre_model_files_df['label'] = pre_model_files_df.apply(lambda row: row.files.split('-')[3], axis = 1)

    pre_model_files_df_dedup = pre_model_files_df.drop_duplicates(subset='ind')
    pre_model_files_df = pre_model_files_df.set_index(['ind'])

    pre_model_files.sort()
    label_names.sort()

    pre_model_files_arr = np.array(pre_model_files)
    label_names_arr = np.array(label_names)

    labels = [np.argmax(np.load(os.path.join(use_path, label_name))) for label_name in label_names]
    best_weights = []
    last_weights = []

    if project['kfold'] >= 3:
        kfold = StratifiedKFold(n_splits=project['kfold'], shuffle=True, random_state = project['seed'])
        kfold_generator = kfold.split(pre_model_files_df_dedup, pre_model_files_df_dedup.label)
        validate = True
    else:
        print('Too few k-folds selected, fitting on all data')
        kfold_generator = no_folds_generator(pre_model_files_df_dedup)
        validate = False

    for i, (train, test) in enumerate(kfold_generator):
        if project['kfold_every']:
            print('----- Fitting Fold', i, '-----')
        elif i > 0:
            break


        weights_name = project['name'] + weight_label + '-kfold-' + str(i) + '-round-' + str(project['model_round']) +'.hdf5'
        plot_name = project['name'] + weight_label + '-kfold-' + str(i) + '-round-' + str(project['model_round']) +'.png'

        if project[weights] is None:
            fold_weights = None
        else:
            fold_weights = project[weights][i]
        if final:
            if project['architecture'] == 'resnet50':
                model = get_resnet_final_model(img_dim, conv_dim, project['number_categories'], fold_weights, project['is_final'])
            elif project['architecture'] == 'xception':
                model = get_xception_final_model(img_dim, conv_dim, project['number_categories'], fold_weights, project['is_final'])
            else:
                model = get_inception_v3_final_model(img_dim, conv_dim, project['number_categories'], fold_weights, project['is_final'])

            for i, layer in enumerate(model.layers[1].layers):
                if len(layer.trainable_weights) > 0:
                    if i < project['final_cutoff']:
                        mult = 0.01
                    else:
                        mult = 0.1
                    layer.learning_rate_multiplier = [mult for tw in layer.trainable_weights]

        else:
            if project['architecture'] == 'resnet50':
                pre_model, model = get_resnet_pre_post_model(img_dim,
                                                    conv_dim,
                                                    len(project['categories']),
                                                    model_weights = fold_weights)
            elif project['architecture'] == 'xception':
                pre_model, model = get_xception_pre_post_model(img_dim,
                                                    conv_dim,
                                                    len(project['categories']),
                                                    model_weights = fold_weights)
            else:
                pre_model, model = get_inception_v3_pre_post_model(img_dim,
                                                    conv_dim,
                                                    len(project['categories']),
                                                    model_weights = fold_weights)

        pre_model_files_dedup_train = pre_model_files_df_dedup.iloc[train]
        train_ind = list(set(pre_model_files_dedup_train.ind))
        pre_model_files_train = pre_model_files_df.loc[train_ind]

        gen_train = gen_minibatches(use_path, pre_model_files_train.files, project['batch_size'], project['architecture'], final = final)
        number_train_samples = len(pre_model_files_train)

        if validate:
            pre_model_files_dedup_test = pre_model_files_df_dedup.iloc[test]
            test_ind = list(set(pre_model_files_dedup_test.ind))
            pre_model_files_test = pre_model_files_df.loc[test_ind]

            gen_test = gen_minibatches(use_path, pre_model_files_test.files, project['batch_size'], project['architecture'], final = final)
            number_test_samples = len(pre_model_files_test)
            validation_steps = (number_test_samples // project['batch_size'])

            weights_checkpoint_file = weights_name.split('.')[0] + '-kfold-' + str(i) + "-improvement-{epoch:02d}-{val_categorical_accuracy:.4f}.hdf5"
            checkpoint = ModelCheckpoint(os.path.join(weights_path, weights_checkpoint_file),
                                        monitor='val_categorical_accuracy',
                                        verbose=1,
                                        save_best_only=True,
                                        mode='max')

            callbacks_list = [checkpoint]
        else:
            gen_test = None
            validation_steps = None
            callbacks_list = None


        steps_per_epoch = (number_train_samples // project['batch_size'])
        for j in range(project['rounds']):
            optimizer = Adam(lr = lr, decay = decay)

            model.compile(optimizer = optimizer,
                        loss = 'categorical_crossentropy',
                        metrics = ['categorical_accuracy'])

            model.fit_generator(gen_train,
                                steps_per_epoch = steps_per_epoch,
                                epochs = project['cycle'] * (j + 1),
                                verbose = 1,
                                validation_data = gen_test,
                                validation_steps = validation_steps,
                                initial_epoch = j * project['cycle'],
                                callbacks = callbacks_list)

        model.save_weights(os.path.join(weights_path, weights_name))
        last_weights.append(os.path.join(weights_path, weights_name))
        weights_names = os.listdir(weights_path)
        max_val = -1
        max_i = -1
        for j, name in enumerate(weights_names):
            if name.find(weights_name.split('.')[0]) >= 0:
                if (name.find(weight_label) >= 0) and (name.find('improvement') >= 0):
                    val = int(name.split('.')[1])
                    if val > max_val:
                        max_val = val
                        max_i = j
        if project['plot']:
            print('Plotting confusion matrix')

            if max_i == -1:
                print('Loading last weights:', os.path.join(weights_path, weights_name))
                model.load_weights(os.path.join(weights_path, weights_name))
            else:
                print('Loading best weights:', os.path.join(weights_path, weights_names[max_i]))
                model.load_weights(os.path.join(weights_path, weights_names[max_i]))
            best_predictions = []
            true_labels = []

            print('Predicting test files')
            if validate:
                use_files = pre_model_files_test.files
            else:
                use_files = pre_model_files_train.files
            for array_name in tqdm(use_files):
                img_path = os.path.join(use_path, array_name)
                img = np.load(img_path)
                if final:
                    if project['architecture'] == 'resnet50':
                        img = np.squeeze(resnet_preprocess_input(img[np.newaxis].astype(np.float32)))
                    elif project['architecture'] == 'xception':
                        img = np.squeeze(xception_preprocess_input(img[np.newaxis].astype(np.float32)))
                    else:
                        img = np.squeeze(inception_v3_preprocess_input(img[np.newaxis].astype(np.float32)))
                prediction = model.predict(img[np.newaxis])
                best_predictions.append(project['categories'][np.argmax(prediction)])
                true_label = np.load(img_path.replace('-img-','-label-'))
                true_labels.append(project['categories'][np.argmax(true_label)])

            cm = confusion_matrix(true_labels, best_predictions, project['categories'])
            plt.clf()
            sns.heatmap(pd.DataFrame(cm, project['categories'], project['categories']), annot = True, fmt = 'g')
            plt.xlabel('Actual')
            plt.xlabel('Predicted')
            plt.xticks(rotation = 45, fontsize = 8)
            plt.yticks(rotation = 45, fontsize = 8)
            plt.title('Confusion matrix for fold: ' + str(i) + '\nweights' + weights_name)
            plt.savefig(os.path.join(plot_path, plot_name))
            print('Confusion matrix plot saved:', colored(os.path.join(plot_path, plot_name), 'magenta'))


        if max_i == -1:
            best_weights.append(os.path.join(weights_path, weights_name))
        else:
            best_weights.append(os.path.join(weights_path, weights_names[max_i]))

    project['number_categories'] = len(project['categories'])
    project['best_weights'] = best_weights
    project['last_weights'] = last_weights
    project['is_final'] = final

    return project
Example #15
0
def xception_preprocess_image(resized_image):
    preprocessed_image = xception_preprocess_input(resized_image)
    return preprocessed_image
def preprocess_image(image_path, image_size=(224, 224)):
    original_image = image.load_img(image_path, target_size=image_size)
    img = image.img_to_array(original_image)
    img = np.expand_dims(img, axis=0)
    img = xception_preprocess_input(img)
    return original_image, img