def get_train_test_lda(topic):
    model = VGG16(include_top=False, pooling='avg')

    x_train, y_train, x_test, y_test = load()

    x_train = x_train.astype('float32')
    x_train /= 255

    y_train = y_train.astype('int64')

    x_test = x_test.astype('float32')
    x_test /= 255
    y_test = y_test.astype('float32')

    X_train = model.predict(x_train)
    print(X_train.shape)
    X_test = model.predict(x_test)
    # X_train = model.predict(x_train)
    # X_test = model.predict(x_test)

    for k in topic:
        X_iter = X_train

        model_label = lda.LDA(n_topics=k, n_iter=1000)
        model_label.fit(y_train)
        doc_topic = model_label.doc_topic_
        x2 = doc_topic

        x = x2
        x = discretization_doc_topic(x)
        X_train = np.hstack((X_train, x))

        # multi-label learning to get x2
        classifier = LabelPowerset(RandomForestClassifier())
        classifier.fit(X_iter, x)

        x = np.array(sp.csr_matrix(classifier.predict(X_test)).toarray())
        # print(x)
        # x = alpha * x1 + (1-alpha) * x2
        # x = self.discretization_doc_topic(x)
        X_test = np.hstack((X_test, x))

    return np.array(X_train)[:, -28:], np.array(y_train), np.array(
        X_test)[:, -28:], np.array(y_test)
Created on Tue Jul 25 14:50:44 2017

@author: jingang
"""

from getdata import load
import tensorflow as tf
import time
import numpy as np
from datetime import timedelta
import matplotlib.pyplot as plt
from PIL import Image
import PIL
import cv2

x_train, x_test, y_train, y_test = load()

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

x_train /= 255
x_test /= 255

x_train = np.transpose(x_train, (0, 2, 3, 1))
x_test = np.transpose(x_test, (0, 2, 3, 1))

# change the dimension of training and testing images

# define img size
img_size = 100
img_shape = (img_size, img_size)
Ejemplo n.º 3
0
from keras.callbacks import ModelCheckpoint
from sklearn.metrics import matthews_corrcoef
from sklearn.metrics import hamming_loss
from keras import backend as k
from keras.layers import Dense, GlobalAveragePooling2D
from keras.applications.vgg16 import VGG16
import tensorflow as tf
import warnings
import os
from sklearn.metrics import accuracy_score, hamming_loss, average_precision_score, f1_score, recall_score, precision_score
from keras.callbacks import TensorBoard
warnings.filterwarnings("ignore")

os.environ["CUDA_VISIBLE_DEVICES"] = "0"

x_train, y_train, x_val, y_val = load()

#get topic
#input1, input2 = get_train_test_lda(y_train, y_val, [7])

x_train = x_train.astype('float32')
x_val = x_val.astype("float32")
#input1 = input1.astype('float32')
#input2 = input2.astype('float32')

x_train /= 255
x_val /= 255

print("model running...")
print("load VGG16 network...")
base_model = VGG16(weights='imagenet', include_top=False)
Ejemplo n.º 4
0
Links:
    - [AlexNet Paper](http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf)
    - [Flower Dataset (17)](http://www.robots.ox.ac.uk/~vgg/data/flowers/17/)
"""

from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

import getdata

X, test_X, Y, test_Y = getdata.load()
# Building 'AlexNet'
network = input_data(shape=[None, 227, 227, 3])
network = conv_2d(network, 96, 11, strides=4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 256, 5, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
Ejemplo n.º 5
0
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras import applications
from keras.optimizers import SGD, adam

epochs = 1
batch_size = 32
top_model_weights_path = 'bottleneck_fc_model.h5'

# path to the model weights files.
# weights_path = '../keras/examples/vgg16_weights.h5'
top_model_weights_path = 'bottleneck_fc_model_0.h5'
# dimensions of our images.
img_width, img_height = 100, 100
submission_file = 'model-06-submission.csv'

x_train, x_test, y_train, y_test = load(test_size=0.2)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

x_train /= 255
x_test /= 255

print(x_train.shape)


def save_bottlebeck_features():
    # build the VGG16 network
    model = applications.VGG16(include_top=False, weights='imagenet')
    bottleneck_features_train = model.predict(x_train,
                                              batch_size=batch_size,
Ejemplo n.º 6
0
def train_full_model():
    # build the VGG16 network
    base_model = applications.VGG16(weights='imagenet',
                                    include_top=False,
                                    input_shape=input_shape)
    print('Model loaded.')
    # base_model.summary()
    # build a classifier model to put on top of the convolutional model
    top_model = Sequential()
    top_model.add(Flatten(input_shape=(4, 4, 512)))
    top_model.add(Dense(256, activation='relu'))
    top_model.add(Dropout(0.5))
    top_model.add(Dense(17, activation='sigmoid'))

    # note that it is necessary to start with a fully-trained
    # classifier, including the top classifier,
    # in order to successfully do fine-tuning
    top_model.load_weights(top_model_weights_path)

    # top_model.summary()

    # add the model on top of the convolutional base
    model = Model(inputs=base_model.input,
                  outputs=top_model(base_model.output))

    # set the first 15 layers (up to the last conv block)
    # to non-trainable (weights will not be updated)
    for layer in model.layers[:15]:
        # print(layer.name)
        layer.trainable = False

    model.summary()
    model.load_weights('weights-model-07.01-0.95972.hdf5')

    # compile the model with a SGD/momentum optimizer
    # and a very slow learning rate.
    model.compile(loss='binary_crossentropy',
                  optimizer=adam(lr=1e-4),
                  metrics=['accuracy'])

    x_train, x_test, y_train, y_test = load(dataset='train-dataset-128.h5',
                                            test_size=0.3)

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')

    x_train /= 255
    x_test /= 255

    # tbCallBack = TensorBoard(log_dir='/Graph', histogram_freq=1, write_graph=True, write_images=True, embeddings_freq=1)
    check = ModelCheckpoint("weights-model-07.{epoch:02d}-{val_acc:.5f}.hdf5",
                            monitor='val_acc',
                            verbose=1,
                            save_best_only=True,
                            save_weights_only=True,
                            mode='auto')
    model.fit(x_train,
              y_train,
              batch_size=batch_size,
              epochs=epochs,
              callbacks=[check],
              validation_data=(x_test, y_test))

    ## Predict
    print('Predictions...')
    x_test = test_load('test-dataset-128.h5')
    x_test = x_test.astype('float32')
    x_test /= 255.

    best_threshold = [0.2] * 17

    # print("best_threshold: {}".format(best_threshold))

    pred = model.predict(x_test, verbose=1, batch_size=8)
    # print(pred)
    print(pred.shape)

    classes = [
        'haze', 'primary', 'agriculture', 'clear', 'water', 'habitation',
        'road', 'cultivation', 'slash_burn', 'cloudy', 'partly_cloudy',
        'conventional_mine', 'bare_ground', 'artisinal_mine', 'blooming',
        'selective_logging', 'blow_down'
    ]

    y_pred = []

    ##text=List of strings to be written to file
    with open(submission_file, 'w') as file:
        file.write("image_name,tags")
        file.write('\n')

        for i in range(pred.shape[0]):
            y_pred = np.array([
                1 if pred[i, j] >= best_threshold[j] else 0
                for j in range(pred.shape[1])
            ])
            # print(y_pred)

            # extracting actual class name
            y_pred = [classes[i] for i in range(17) if y_pred[i] == 1]
            y_pred = " ".join([str(item) for item in y_pred])
            # print(y_pred)
            line = "test_{},{}".format(i, y_pred)
            file.write(line)
            file.write('\n')
Ejemplo n.º 7
0
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras import applications
from keras.optimizers import adam

epochs = 3
batch_size = 8
bottleneck_features_train_path = 'bottleneck_features_train_128_vgg.npy'
bottleneck_features_validation_path = 'bottleneck_features_validation_128_vgg.npy'
input_shape = (128, 128, 3)
# path to the model weights files.
# weights_path = '../keras/examples/vgg16_weights.h5'
top_model_weights_path = 'bottleneck_fc_model_128_vgg.h5'
# dimensions of our images.
submission_file = 'model-07-submission.csv'

x_train, x_test, y_train, y_test = load(dataset='train-dataset-128.h5',
                                        test_size=0.3)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

x_train /= 255
x_test /= 255

print(x_train.shape)


def save_bottlebeck_features():
    # build the InceptionV3 network
    model = applications.VGG16(include_top=False, weights='imagenet')
    model.summary()
    bottleneck_features_train = model.predict(x_train,
import numpy as np
from getdata import load
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint
from sklearn.metrics import matthews_corrcoef
from sklearn.metrics import hamming_loss
from keras import backend as K
K.set_image_dim_ordering('th')

x_train, x_test, y_train, y_test = load()

x_train = x_train.astype('float32')
x_test  = x_test.astype('float32')

x_train /= 255
x_test /= 255


model = Sequential()
model.add(Convolution2D(32, kernel_size=(3, 3),padding='same',input_shape=(3 , 100, 100)))
model.add(Activation('relu'))
model.add(Convolution2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Convolution2D(64,(3, 3), padding='same'))
model.add(Activation('relu'))