Ejemplo n.º 1
0
def test_resnet3d_152(resnet3d_test):
    """Test 152."""
    K.set_image_data_format('channels_last')
    model = Resnet3DBuilder.build_resnet_152((224, 224, 224, 1), 2)
    resnet3d_test(model)
    K.set_image_data_format('channels_first')
    model = Resnet3DBuilder.build_resnet_152((1, 512, 512, 256), 2)
    resnet3d_test(model)
Ejemplo n.º 2
0
def test_get_block():
    """Test get residual block."""
    K.set_image_data_format('channels_last')
    Resnet3DBuilder.build((224, 224, 224, 1), 2, 'bottleneck',
                          [2, 2, 2, 2], reg_factor=1e-4)
    assert True
    with pytest.raises(ValueError):
        Resnet3DBuilder.build((224, 224, 224, 1), 2, 'nullblock',
                              [2, 2, 2, 2], reg_factor=1e-4)
Ejemplo n.º 3
0
def test_resnet3d_18():
    K.set_image_data_format('channels_last')
    model = Resnet3DBuilder.build_resnet_18((224, 224, 224, 1), 2)
    model.compile(loss="categorical_crossentropy", optimizer="sgd")
    assert True, "Failed to build with tensorflow"

    K.set_image_data_format('channels_first')
    model = Resnet3DBuilder.build_resnet_18((1, 512, 512, 256), 2)
    model.compile(loss="categorical_crossentropy", optimizer="sgd")
    assert True, "Failed to build with theano"
Ejemplo n.º 4
0
def create_reg_resnet18_3D(img_x, img_y, ch_n, seq_len, tgt_size):
    resnet18_3D = Resnet3DBuilder.build_resnet_18(
        (seq_len, img_x, img_y, ch_n), tgt_size)
    resnet18_3D.layers.pop()
    layer = Dense(32, activation='relu')(resnet18_3D.output)
    resnet18_3D.layers[-1].outbound_nodes = []
    resnet18_3D.outputs = [resnet18_3D.layers[-1].output]
    output = resnet18_3D.get_layer('flatten_1').output
    output = Dense(32, activation='relu')(output)
    output = Dense(1, activation='linear')(output)
    reg_resnet18_3D = Model(resnet18_3D.input, output)
    return reg_resnet18_3D
Ejemplo n.º 5
0
Archivo: train.py Proyecto: gamcoh/Dina
            batch_x.append(frames)
            batch_y.append(labels)

        batch_x = np.array(batch_x)
        batch_y = np.array(batch_y)

        batch_x = batch_x.reshape(-1, 30, 64, 96, 3)

        yield batch_x, batch_y


train_generator = generator('train', batch_size=64)
validation_generator = generator('val', batch_size=64)

model = Resnet3DBuilder.build_resnet_101((30, 64, 96, 3), 27)
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
callbacks = [
    ModelCheckpoint('./resnet3d_50_dropout_3c_64x96_b32.h5',
                    monitor='val_accuracy',
                    verbose=1,
                    save_best_only=True),
    EarlyStopping(patience=5)
]
history = model.fit(train_generator,
                    validation_data=validation_generator,
                    steps_per_epoch=119_000 // 64,
                    validation_steps=15_000 // 64,
                    callbacks=callbacks,
Ejemplo n.º 6
0
    **init_args['volume_image_data_generator']['val']['init'])

train_vol_loader = NPYDataLoader(
    **init_args['volume_image_data_loader']['train'])
val_vol_loader = NPYDataLoader(**init_args['volume_image_data_loader']['val'])

train_iter_args = init_args['volume_image_data_generator']['train'][
    'flow_from_loader']
train_iter_args['volume_image_data_loader'] = train_vol_loader
val_iter_args = init_args['volume_image_data_generator']['val'][
    'flow_from_loader']
val_iter_args['volume_image_data_loader'] = val_vol_loader

image_shape = train_datagen.image_shape
regularization_factor = 1
model = Resnet3DBuilder.build_resnet_18(image_shape, nb_classes,
                                        regularization_factor)
model.load_weights(weights)
compile_args = init_args['model']['compile']
compile_args['optimizer'] = Adam(lr=1e-4)
model.compile(**compile_args)

model_fit_args = init_args['model']['fit_generator']
model_fit_args['generator'] = train_datagen.flow_from_loader(**train_iter_args)
model_fit_args['validation_data'] = val_datagen.flow_from_loader(
    **val_iter_args)
model_fit_args['callbacks'] = [
    checkpointer, lr_reducer, early_stopper, csv_logger
]

model.fit_generator(**model_fit_args)
model.save('output/resnet18_{}_ctd.h5'.format(title))
Ejemplo n.º 7
0
    print("Valid hdf5 file in 'read' mode: " + str(hdf5_file))
    file_size = os.path.getsize(path_to_hdf5)
    print('Size of hdf5 file: {:.3f} GB'.format(file_size/2.0**30))

    num_rows = hdf5_file['input'].shape[0]
    print("There are {} images in the dataset.".format(num_rows))

    print("The datasets within the HDF5 file are:\n {}".format(list(hdf5_file.values())))

    input_shape = tuple(list(hdf5_file["input"].attrs["lshape"]))
    batch_size = args.batchsize   # Batch size to use
    print ("Input shape of tensor = {}".format(input_shape))

    from resnet3d import Resnet3DBuilder

    model = Resnet3DBuilder.build_resnet_18((64, 64, 64, 1), 1)  # (input tensor shape, number of outputs)

    tb_log = keras.callbacks.TensorBoard(log_dir=TB_LOG_DIR,
                                histogram_freq=0,
                                batch_size=batch_size,
                                write_graph=True,
                                write_grads=True,
                                write_images=True,
                                embeddings_freq=0,
                                embeddings_layer_names=None,
                                embeddings_metadata=None)


    checkpointer = keras.callbacks.ModelCheckpoint(filepath=CHECKPOINT_FILENAME,
                                                   monitor="val_loss",
                                                   verbose=1,
Ejemplo n.º 8
0
copy(__file__, output_dir)

# Download the data from: /hpf/largeprojects/ccm/devin/plastics-data/???
datapath = "data/data.h5"
test_size = 0.20
X_train, X_test, Y_train, Y_test = create_train_test(datapath, test_size)


print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))

model = Resnet3DBuilder.build_resnet_50((128, 128, 128, 1), 3)
# model = Resnet3DBuilder.build((128, 128, 128, 1), 3, basic_block, [1, 1, 1, 1], reg_factor=1e-4)

# Uncomment the following block to do transfer learning!
model.layers[-1].name = "dense_resnet_1" # Rename final dense layer so correct number of output classes is used (3 instead of MNIST 10)
# MNIST build_resnet_18
# model.load_weights('/home/carsonmclean/dev/csc2541/csc2541/output/04-03-03:37/model.h5',
#                    by_name = True)
# MNIST [1,1,1,1]
# model.load_weights('/home/carsonmclean/dev/csc2541/csc2541/output/04-09-00:25/model.h5',
#                    by_name = True)

# ModelNet40/PointNet build_resnet_18 dim = 64
# model.load_weights('/home/carsonmclean/dev/csc2541/csc2541/output/04-09-17:40/model.h5',
#                    by_name = True)
# ModelNet40/PointNet build_resnet_18 dim = 128
Ejemplo n.º 9
0
               #print (len(y_train))
               del labels
               # batch_size =1
               x_val = np.array(x_val)
               #print ("byeeeee",x_train.shape[0])
               y_val = np.array(y_val)
               print ('=++=++=++=++=',y_val)
               x_val,y_val = shuffle(x_val,y_val)
               #print ("////////",x_val)
               x_val = x_val.astype("float32")
               y_val = y_val.astype("float32")
               x_val /= 255
               #y_val /= 255
               yield x_val, y_val
               x_val = []
               y_val = []

from resnet3d import Resnet3DBuilder

model = Resnet3DBuilder.build_resnet_10((3, 224, 224, 3), 101)
model.summary()
#optimizer = keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0)
optimizer = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['categorical_accuracy'])
#model.fit_generator(dataGenerator(train_lines),steps_per_epoch=7058, epochs=2)

model.fit_generator(generator=dataGenerator(train_lines), validation_data=val_dataGenerator(val_lines), validation_steps=2472, steps_per_epoch=7058, epochs=5)

Ejemplo n.º 10
0
        y_filename = os.path.join(y_path, filename)

        X = np.load(X_fileanme)
        y = np.load(y_filename)

        for i in range(X.shape[0] // batch_size):
            indexes = np.random.randint(X.shape[0], size=batch_size)
            yield X[indexes], y[indexes]

        del X
        del y

if __name__ == '__main__':
    # model, training_model = get_unet_model()
    # model, training_model = get_model()
    model = Resnet3DBuilder.build_resnet_50((64, 64, 16, 1), 2)
    training_model = keras.utils.multi_gpu_model(model)
    training_model.compile(optimizer=Adam(amsgrad=True), loss='binary_crossentropy', metrics=['accuracy'])
    model.summary()

    callbacks=[
        RedirectModel(keras.callbacks.ModelCheckpoint(
            os.path.join(
                './model_checkpoints',
                '{epoch:02d}.h5'
            ),
            verbose=1,
        ), model),
        keras.callbacks.TensorBoard(
            log_dir='./logs/' + datetime.datetime.now().strftime('%Y%m%d%H%M')
        ),
Ejemplo n.º 11
0
import numpy as np
from resnet3d import Resnet3DBuilder

# pseudo volumetric data
X_train = np.random.rand(10, 64, 64, 32, 1)
labels = np.random.randint(0, 2, size=[10])
y_train = np.eye(2)[labels]

# train
model = Resnet3DBuilder.build_resnet_18((64, 64, 32, 1), 2)
model.compile(loss="categorical_crossentropy", optimizer="sgd")
model.fit(X_train, y_train, batch_size=10)
Ejemplo n.º 12
0
## convert to 1 + 4D space (1st argument represents number of rows in the dataset)
X_train = X_train.reshape(X_train.shape[0], 16, 16, 16, 1)
X_test = X_test.reshape(X_test.shape[0], 16, 16, 16, 1)
Y_train = to_categorical(Y_train, 10)
Y_test = to_categorical(Y_test, 10)

print("number of training examples = " + str(X_train.shape[0]))
print("number of test examples = " + str(X_test.shape[0]))
print("X_train shape: " + str(X_train.shape))
print("Y_train shape: " + str(Y_train.shape))
print("X_test shape: " + str(X_test.shape))
print("Y_test shape: " + str(Y_test.shape))

# model = Resnet3DBuilder.build_resnet_18((16, 16, 16, 1), 10)
model = Resnet3DBuilder.build((16, 16, 16, 1),
                              10,
                              basic_block, [1, 1, 1, 1],
                              reg_factor=1e-4)
adam = Adam(lr=0.0001)
model.compile(optimizer=adam,
              loss='categorical_crossentropy',
              metrics=['accuracy'])
earlystop = EarlyStopping(monitor='val_acc',
                          min_delta=0.001,
                          patience=5,
                          verbose=2,
                          restore_best_weights=True)
callback_list = [earlystop]

model_info = model.fit(X_train,
                       Y_train,
                       epochs=100,
Ejemplo n.º 13
0
import numpy as np
from resnet3d import Resnet3DBuilder

# pseudo volumetric data
X_train = np.random.rand(10, 64, 64, 32, 1)
labels = np.random.randint(0, 2, size=[10])
y_train = np.eye(2)[labels]

# train
model = Resnet3DBuilder.build_resnet_50((64, 64, 32, 1), 2, multilabel=True)
model.compile(loss="categorical_crossentropy", optimizer="sgd")
model.fit(X_train, y_train, batch_size=10)
Ejemplo n.º 14
0
copy(__file__, output_dir)

datapath = "data/modelnet/modelnet40_ply_hdf5_2048/"
test_size = 0.25
X_train, X_test, Y_train, Y_test = create_train_test(datapath, test_size)


print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))

dim = 64
model = Resnet3DBuilder.build_resnet_50((dim, dim, dim, 1), 40)
# model = Resnet3DBuilder.build((128, 128, 128, 1), 3, basic_block, [1, 1, 1, 1], reg_factor=1e-4)

# Uncomment the following block to do transfer learning!
# model.layers[-1].name = "dense_resnet_1" # Rename final dense layer so correct number of output classes is used (3 instead of MNIST 10)
# model.load_weights('/home/carsonmclean/dev/csc2541/csc2541/output/04-03-03:37/model.h5',
#                    by_name = True) # build_resnet_18
# model.load_weights('/home/carsonmclean/dev/csc2541/csc2541/output/04-09-00:25/model.h5',
#                    by_name = True) # [1,1,1,1]
# End of transfer learning

adam = Adam(lr=0.00001)
model.compile(optimizer=adam,
              loss='categorical_crossentropy',
              metrics=['accuracy'])
earlystop = EarlyStopping(monitor = 'val_acc',
Ejemplo n.º 15
0
def test_bad_shape():
    """Input shape need to be 4."""
    K.set_image_data_format('channels_last')
    with pytest.raises(ValueError):
        Resnet3DBuilder.build_resnet_152((224, 224, 224), 2)
Ejemplo n.º 16
0
    def model_confirm(self, choosed_model):
        if choosed_model == 'VGG16':
            model = MODEL(self.config).VGG16()
        elif choosed_model == 'VGG19':
            model = MODEL(self.config).VGG19()
        elif choosed_model == 'ResNet3D':
            model = Resnet3DBuilder().build_resnet_18((91, 109, 91, 1), 2)
        elif choosed_model == 'AlexNet':
            model = MODEL(self.config).AlexNet()
        elif choosed_model == 'LeNet':
            model = MODEL(self.config).LeNet()
        elif choosed_model == 'ZF_Net':
            model = MODEL(self.config).ZF_Net()
        elif choosed_model == 'ResNet18':
            model = ResnetBuilder().build_resnet18(self.config)
        elif choosed_model == 'ResNet34':
            model = ResnetBuilder().build_resnet34(self.config)
        elif choosed_model == 'ResNet101':
            model = ResnetBuilder().build_resnet101(self.config)
        elif choosed_model == 'ResNet152':
            model = ResnetBuilder().build_resnet152(self.config)
        elif choosed_model == 'mnist_net':
            model = MODEL(self.config).mnist_net()
        elif choosed_model == 'TSL16':
            model = MODEL(self.config).TSL16()
        elif choosed_model == 'ResNet50':
            model = keras.applications.ResNet50(include_top=True,
                                                weights=None,
                                                input_tensor=None,
                                                input_shape=(self.normal_size,
                                                             self.normal_size,
                                                             self.channles),
                                                pooling='max',
                                                classes=self.classNumber)
        elif choosed_model == 'InceptionV3':
            model = keras.applications.InceptionV3(
                include_top=True,
                weights=None,
                input_tensor=None,
                input_shape=(self.normal_size, self.normal_size,
                             self.channles),
                pooling='max',
                classes=self.classNumber)

        elif choosed_model == 'Xception':
            model = keras.applications.Xception(include_top=True,
                                                weights=None,
                                                input_tensor=None,
                                                input_shape=(self.normal_size,
                                                             self.normal_size,
                                                             self.channles),
                                                pooling='max',
                                                classes=self.classNumber)
        elif choosed_model == 'MobileNet':
            model = keras.applications.MobileNet(include_top=True,
                                                 weights=None,
                                                 input_tensor=None,
                                                 input_shape=(self.normal_size,
                                                              self.normal_size,
                                                              self.channles),
                                                 pooling='max',
                                                 classes=self.classNumber)
        # elif choosed_model == 'DenseNet':
        #     depth = 40
        #     nb_dense_block = 3
        #     growth_rate = 12
        #     nb_filter = 12
        #     bottleneck = False
        #     reduction = 0.0
        #     dropout_rate = 0.0
        #
        #     img_dim = (self.channles, self.normal_size) if K.image_dim_ordering() == "th" else (
        #         self.normal_size, self.normal_size, self.channles)
        #
        #     model = densenet.DenseNet(img_dim, classNumber=self.classNumber, depth=depth, nb_dense_block=nb_dense_block,
        #                               growth_rate=growth_rate, nb_filter=nb_filter, dropout_rate=dropout_rate,
        #                               bottleneck=bottleneck, reduction=reduction, weights=None)

        return model