def create_train(dataset_info, batch_size, shape, augument=True, oversample_factor = 0):
        assert shape[2] == 3

        if oversample_factor > 0:

            rare_dataset_info = np.array([item for item in dataset_info if np.isin(item['labels'], rare_classes).any()])
            for i in range(oversample_factor):
                dataset_info = np.append(dataset_info,rare_dataset_info)
        while True:
            dataset_info = shuffle(dataset_info)
            for start in range(0, len(dataset_info), batch_size):
                end = min(start + batch_size, len(dataset_info))
                batch_images = []
                X_train_batch = dataset_info[start:end]
                batch_labels = np.zeros((len(X_train_batch), 28))
                for i in range(len(X_train_batch)):
                    image = data_generator.load_image(X_train_batch[i]['path'])

                    #rare = np.isin(X_train_batch[i]['labels'], rare_classes).any()

                    if augument:
                        image = data_generator.augment(normal_aug,image)
                    image = preprocess_input(image)
                    batch_images.append(image)
                    batch_labels[i][X_train_batch[i]['labels']] = 1
                yield np.array(batch_images, np.float32), batch_labels
Example #2
0
def result(img, model):
    x = resize(img, (224, 224)) * 255
    x = preprocess_input(x)
    x = np.expand_dims(x, 0)
    y = model.predict(x)
    y = np.mean(y, axis=(1, 2))
    return y
Example #3
0
import matplotlib.pyplot as plt
import pickle
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import numpy as np

# prepare your data
X = pickle.load(open("x_total.pickle", "rb"))
y = pickle.load(open("y_total.pickle", "rb"))

n_classes = 36
y = np.eye(n_classes)[np.asarray(y)]

X = np.asarray(X)

X = preprocess_input(X)

X, y = shuffle(X, y, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.20,
                                                    random_state=5)

# build model
base_model = ResNet18(input_shape=(224, 224, 3),
                      weights='imagenet',
                      include_top=False)
x = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(n_classes, activation='softmax')(x)
model = keras.models.Model(inputs=[base_model.input], outputs=[output])
model.summary()
 def load_image(path):
     image = cv2.imread(path + '.png', cv2.IMREAD_UNCHANGED)
     image = preprocess_input(image)
     return image
Example #5
0

def initiate_model():
    model = ResNet50((224, 224, 3), weights='imagenet')
    target_layer = [l for l in model.layers if l.name == 'pool1'][0]
    hidden_extractor = Model(model.input, target_layer.output)
    hidden_extractor.compile(loss='mean_squared_error', optimizer='sgd')
    return hidden_extractor


def preprocess(f_n, cs=[0, 4, 15, 19]):
    dat = h5py.File(f_n, 'r')['masked_mat']
    if cs is None:
        cs = np.arange(dat.shape[2])
    stacks = []
    for c in cs:
        patch_c = cv2.resize(np.array(dat[:, :, c]).astype(float), (224, 224))
        stacks.append(np.stack([patch_c] * 3, 2))
    return np.stack(stacks, 0)


if __name__ == '__main__':
    fs = read_file_path(DATA_ROOT)
    extractor = initiate_model()
    for f_n in fs:
        print("Processing %s" % f_n)
        x = preprocess_input(preprocess(f_n))
        y = extractor.predict(x)
        f_n_out = f_n.replace("StaticPatches", "EncodedStaticPatches")
        np.save(f_n_out, y)
Example #6
0
from tensorflow.python.client import device_lib
from keras import backend as K
K.tensorflow_backend._get_available_gpus()

os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
# The GPU id to use, usually either "0" or "1"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

# print("---wait---\n")
# print(device_lib.list_local_devices())

# 이미지 불러오기
x = cv2.imread('./imgs/tests/4.jpg')
x_image = cv2.resize(x, (224, 224))
x_img_rgb = cv2.cvtColor(x_image, cv2.COLOR_BGR2RGB)
x = preprocess_input(x_image)
x = np.expand_dims(x, 0)

# resnet pre-trained weight 불러오기
# load model - This will take < 10 min since we have to download weights (about 240 Mb)
# model = ResNet152(input_shape=(224,224,3), weights='imagenet', classes=1000)
model = ResNet50(input_shape=(224, 224, 3), weights='imagenet', classes=1000)
# model = ResNet34(input_shape=(224,224,3), weights='imagenet', classes=1000)

# processing image
y = model.predict(x)

# 결과
predictions_array = decode_predictions(y)[0]

# 시각화
    train_generator,
    steps_per_epoch=np.ceil(float(len(train_indexes)) / float(batch_size)),
    validation_data=validation_generator,
    validation_steps=np.ceil(float(len(valid_indexes)) / float(batch_size)),
    epochs=100,
    verbose=1,
    callbacks=callbacks_list)

model.save_weights(MODEL_PATH + 'model_f{}_e{}_bce.h5'.format(fold_id, epochs))
model.load_weights(MODEL_PATH + 'model_f{}_e50.h5'.format(fold_id))
preds = np.zeros(shape=(len(valid_indexes), 28))
preds_05 = np.zeros(shape=(len(valid_indexes), 28))
y_true = np.zeros(shape=(len(valid_indexes), 28))
for i, info in tqdm(enumerate(train_dataset_info[valid_indexes])):
    image = data_generator.load_image(info['path'])
    image = preprocess_input(image)
    score_predict = model.predict(image[np.newaxis])[0]
    thresh = 0.2
    preds[i][score_predict >= thresh] = 1
    preds_05[i][score_predict >= 0.5] = 1
    y_true[i][info['labels']] = 1

from sklearn.metrics import f1_score

#individual_f1_scores = np.zeros(28)
#for i in range(28):
#    individual_f1_scores[i] = f1_score(y_true[:,i],preds[:,i])
#individual_f1_scores = pd.DataFrame(individual_f1_scores,columns=['f1'])
#individual_f1_scores.to_csv(MODEL_PATH + f'summary_f1_score_f{fold_id}.csv',index=False)

f1_res = f1_score(y_true, preds, average='macro')
Example #8
0
    args.testdir += "/"

train_path_list = glob.glob(args.traindir + '*.jpg')
test_path_list = glob.glob(args.testdir + '*.jpg')

train_img_data = []
train_img_labels = []
test_img_data = []
test_img_labels = []

# Read train and test images and labels
for path in train_path_list:
    classname = path.split('_')[-1][:-4]
    x = imread(path)
    x = resize(x, (img_size, img_size)) * 255
    x = preprocess_input(x)
    x = np.expand_dims(x, axis=0)
    train_img_data.append(x)
    train_img_labels.append(classname)

for path in test_path_list:
    classname = path.split('_')[-1][:-4]
    x = imread(path)
    x = resize(x, (img_size, img_size)) * 255
    x = preprocess_input(x)
    x = np.expand_dims(x, axis=0)
    test_img_data.append(x)
    test_img_labels.append(classname)

train_img_data = np.asarray(train_img_data)
if train_img_data.shape[0] == 0:
def pre_process_pic(x):
    x = resize(x, (224, 224)) * 255
    x = preprocess_input(x)
    x = np.expand_dims(x, 0)
    return x
Example #10
0
from classification_models import ResNet18, ResNet34, ResNet50, ResNet101, ResNet152
from classification_models import ResNeXt50, ResNeXt101
from classification_models import resnet
from classification_models import resnext


models_zoo = {
    'resnet18': {
        'model': ResNet18,
        'params': [
            {
                'input_shape': (224,224,3),
                'dataset': 'imagenet',
                'ground_truth': [(144, 0.5189058), (23, 0.17232688), (21, 0.098873824), (22, 0.03640686), (315, 0.023893135)],
                'preprocessing_function': lambda x:resnet.preprocess_input(x, (224, 224), True),
            }
        ]
    },
    
    'resnet34': {
        'model': ResNet34,
        'params': [
            {
                'input_shape': (224,224,3),
                'dataset': 'imagenet',
                'ground_truth': [(144, 0.88104683), (23, 0.031556014), (21, 0.024246644), (146, 0.022548646), (94, 0.0057696267)],
                'preprocessing_function': lambda x:resnet.preprocess_input(x, (224, 224), True),
            }
        ]
    },