Example #1
0
def sparse_feature_net_61x61(batch_input_shape=(1, 2, 1080, 1280),
                             n_features=3,
                             reg=0.001,
                             init='he_normal',
                             weights_path=None):

    model = Sequential()

    # Keras 1
    if (parse_version(keras_version) < parse_version('2.0.0')):

        d = 1
        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 batch_input_shape=batch_input_shape,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(
            sparse_Convolution2D(64,
                                 4,
                                 4,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))
        model.add(sparse_MaxPooling2D(pool_size=(2, 2), strides=(d, d)))
        d *= 2

        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))
        model.add(sparse_MaxPooling2D(pool_size=(2, 2), strides=(d, d)))
        d *= 2

        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))
        model.add(sparse_MaxPooling2D(pool_size=(2, 2), strides=(d, d)))
        d *= 2

        model.add(
            sparse_Convolution2D(200,
                                 4,
                                 4,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(TensorProd2D(200, 200, init=init, W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(
            TensorProd2D(200, n_features, init=init, W_regularizer=l2(reg)))
        model.add(Activation(tensorprod_softmax))

    # Keras 2
    else:

        d = 1
        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 batch_input_shape=batch_input_shape,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(
            sparse_Convolution2D(64,
                                 4,
                                 4,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))
        model.add(sparse_MaxPooling2D(pool_size=(2, 2), strides=(d, d)))
        d *= 2

        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))
        model.add(sparse_MaxPooling2D(pool_size=(2, 2), strides=(d, d)))
        d *= 2

        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(
            sparse_Convolution2D(64,
                                 3,
                                 3,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))
        model.add(sparse_MaxPooling2D(pool_size=(2, 2), strides=(d, d)))
        d *= 2

        model.add(
            sparse_Convolution2D(200,
                                 4,
                                 4,
                                 d=d,
                                 init=init,
                                 border_mode='valid',
                                 W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(
            TensorProd2D(200,
                         200,
                         kernel_initializer=init,
                         W_regularizer=l2(reg)))
        model.add(Activation('relu'))

        model.add(
            TensorProd2D(200,
                         n_features,
                         kernel_initializer=init,
                         W_regularizer=l2(reg)))
        model.add(Activation(tensorprod_softmax))

    # read pre-computed weights if provided, and exit
    if weights_path is not None:
        model = set_weights(model, weights_path)
    return model
im5 = np.ones(shape=(1, 2, 200, 200)) + 1
im5[:, :, 0:50, 0:50] = 3

# load one of the DeepCell models
basedatadir = os.path.normpath(
    os.path.join(cytometer.__path__[0], '../data/deepcell'))
netdir = os.path.join(basedatadir, 'trained_networks')
model_dir = os.path.join(netdir, 'HeLa')
model_weights_file = '2017-06-21_HeLa_all_61x61_bn_feature_net_61x61_'

# instantiate model (same for all validation data)
j = 0  # model index
model = deepcell_models.sparse_bn_feature_net_61x61(batch_input_shape=im.shape)
model = deepcell.set_weights(
    model, os.path.join(netdir, model_dir,
                        model_weights_file + str(j) + '.h5'))
im_out = model.predict(im)
im2_out = model.predict(im2)
im3_out = model.predict(im3)
im4_out = model.predict(im4)
im5_out = model.predict(im5)

# add padding to the outputs
im_out = np.pad(im_out,
                pad_width=((0, 0), (0, 0), (30, 30), (30, 30)),
                mode='constant',
                constant_values=0)
im2_out = np.pad(im2_out,
                 pad_width=((0, 0), (0, 0), (30, 30), (30, 30)),
                 mode='constant',
Example #3
0
def sparse_bn_feature_net_31x31(batch_input_shape=(1, 1, 1080, 1280),
                                n_features=3,
                                reg=1e-5,
                                init='he_normal',
                                weights_path=None):

    model = Sequential()
    d = 1
    model.add(
        sparse_Convolution2D(32,
                             4,
                             4,
                             d=d,
                             init=init,
                             batch_input_shape=batch_input_shape,
                             border_mode='valid',
                             W_regularizer=l2(reg)))
    model.add(BatchNormalization(axis=1, mode=2))
    model.add(Activation('relu'))
    model.add(sparse_MaxPooling2D(pool_size=(2, 2), strides=(d, d)))
    d *= 2

    model.add(
        sparse_Convolution2D(64,
                             3,
                             3,
                             d=d,
                             init=init,
                             border_mode='valid',
                             W_regularizer=l2(reg)))
    model.add(BatchNormalization(axis=1, mode=2))
    model.add(Activation('relu'))

    model.add(
        sparse_Convolution2D(64,
                             3,
                             3,
                             d=d,
                             init=init,
                             border_mode='valid',
                             W_regularizer=l2(reg)))
    model.add(BatchNormalization(axis=1, mode=2))
    model.add(Activation('relu'))
    model.add(sparse_MaxPooling2D(pool_size=(2, 2), strides=(d, d)))
    d *= 2

    model.add(
        sparse_Convolution2D(128,
                             3,
                             3,
                             d=d,
                             init=init,
                             border_mode='valid',
                             W_regularizer=l2(reg)))
    model.add(BatchNormalization(axis=1, mode=2))
    model.add(Activation('relu'))

    model.add(
        sparse_Convolution2D(200,
                             3,
                             3,
                             d=d,
                             init=init,
                             border_mode='valid',
                             W_regularizer=l2(reg)))
    model.add(BatchNormalization(axis=1, mode=2))
    model.add(Activation('relu'))

    model.add(TensorProd2D(200, 200, init=init, W_regularizer=l2(reg)))
    model.add(BatchNormalization(axis=1, mode=2))
    model.add(Activation('relu'))

    model.add(TensorProd2D(200, n_features, init=init, W_regularizer=l2(reg)))
    model.add(Activation(tensorprod_softmax))

    if (weights_path != None):
        model = set_weights(model, weights_path)

    return model