Ejemplo n.º 1
0
 def test_load_definition(self):
     K.set_image_dim_ordering("th")
     kmodel, input_data, output_data = TestModels.kmodel_seq_lenet_mnist()
     keras_model_json_path, keras_model_hdf5_path = dump_keras(kmodel, dump_weights=True)
     bmodel = DefinitionLoader.from_json_path(keras_model_json_path)
     WeightLoader.load_weights_from_kmodel(bmodel, kmodel)
     self.assert_allclose(bmodel.forward(input_data), kmodel.predict(input_data))
Ejemplo n.º 2
0
 def test_load_api_with_hdf5(self):
     K.set_image_dim_ordering("th")
     kmodel, input_data, output_data = TestModels.kmodel_graph_1_layer()
     keras_model_json_path, keras_model_hdf5_path = dump_keras(kmodel, dump_weights=True)
     bmodel = BLayer.Model.load_keras(json_path=keras_model_json_path,
                                      hdf5_path=keras_model_hdf5_path)
     self.assert_allclose(kmodel.predict(input_data),
                          bmodel.forward(input_data))
Ejemplo n.º 3
0
 def test_load_weights(self):
     K.set_image_dim_ordering("th")
     kmodel, input_data, output_data = TestModels.kmodel_graph_1_layer()
     keras_model_json_path, keras_model_hdf5_path = dump_keras(kmodel, dump_weights=True)
     bmodel = DefinitionLoader.from_json_path(keras_model_json_path)
     kmodel.set_weights([kmodel.get_weights()[0] + 100, kmodel.get_weights()[1]])
     WeightLoader.load_weights_from_hdf5(bmodel, kmodel, filepath=keras_model_hdf5_path)
     self.assert_allclose(bmodel.forward(input_data), kmodel.predict(input_data))
def set_img_format():
    try:
        if K.backend() == 'theano':
            K.set_image_data_format('channels_first')
        else:
            K.set_image_data_format('channels_last')
    except AttributeError:
        if K._BACKEND == 'theano':
            K.set_image_dim_ordering('th')
        else:
            K.set_image_dim_ordering('tf')
Ejemplo n.º 5
0
    def setup_method(self, method):
        """
        Setup any state tied to the execution of the given method in a class.
        It is invoked for every test method of a class.
        """
        K.set_image_dim_ordering("th")
        sparkConf = create_spark_conf().setMaster("local[4]").setAppName("zoo test case")
        self.sc = get_spark_context(sparkConf)
        self.sc.setLogLevel("ERROR")

        self.sqlContext = SQLContext(self.sc)
        init_engine()
Ejemplo n.º 6
0
def cifar(): #maybe change border mode, idk; also maybe add BatchNormalization(axis=1)

    # Determine proper input shape
    K.set_image_dim_ordering('th')
    input_shape = (1, 256, 192)
    img_input = Input(shape=input_shape)

    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_1')(img_input)
    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_2')(x)
    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_3')(x)
    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_4')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x)
    x = Dropout(0.25)(x)

    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_1')(x)
    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_2')(x)
    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_3')(x)
    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_4')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(x)
    x = Dropout(0.25)(x)

    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_1')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_2')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_3')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_4')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_5')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(x)
    x = Dropout(0.25)(x)

    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_1')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_2')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_3')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_4')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_5')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_6')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(x)
    x = Dropout(0.25)(x)

    x = Flatten(name='flatten')(x)
    x = Dense(1000, activation='relu', name='fc1')(x)
    x = Dropout(0.5)(x)
    x = Dense(1000, activation='relu', name='fc2')(x)
    x = Dropout(0.5)(x)
    x = Dense(2, activation='softmax', name='pred')(x)

    # Create model.
    model = Model(img_input, x)

    #weights=''
    #model.load_weights(weights)

    return model
Ejemplo n.º 7
0
    def __init__(self, *args, **kwargs):
        from keras.layers.core import Dense, Flatten
        from keras.layers.convolutional import Convolution2D
        from keras import backend as K
        if K.backend() == 'theano':
            K.set_image_dim_ordering('tf')
        self.Dense = Dense
        self.Flatten = Flatten
        self.Convolution2D = Convolution2D

        self.kernel = 4
        self.stride = (2, 2)
        super(ConvDQN, self).__init__(*args, **kwargs)
Ejemplo n.º 8
0
 def test_batchnormalization(self):
     K.set_image_dim_ordering("th")
     input_data = np.random.random_sample([2, 5, 32, 32])
     blayer = BLayer.BatchNormalization(axis=1, input_shape=(5, 32, 32))
     klayer = KLayer.BatchNormalization(axis=1, input_shape=(5, 32, 32))
     self.compare_newapi(klayer, blayer, input_data,
                         WeightsConverter.convert_batchnormalization)
     K.set_image_dim_ordering("tf")
     input_data2 = np.random.random_sample([2, 32, 32, 4])
     blayer = BLayer.BatchNormalization(axis=-1, dim_ordering="tf", input_shape=(32, 32, 4))
     klayer = KLayer.BatchNormalization(axis=-1, input_shape=(32, 32, 4))
     self.compare_newapi(klayer, blayer, input_data2,
                         WeightsConverter.convert_batchnormalization)
Ejemplo n.º 9
0
def foo():

    # Determine proper input shape
	if keras.__version__ > '1.0.3':
		K.set_image_dim_ordering('th')
	input_shape = (1, 224, 224)

	#img_input = Input(shape=input_shape)

	model = Sequential()

	model.add(Convolution2D(32, 8, 8,
			        input_shape=input_shape,init=weight_init, name='conv1_1'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(32, 6, 6,init=weight_init, name='conv1_2'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(32, 4, 4,init=weight_init, name='conv1_3'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(32, 2, 2,init=weight_init, name='conv1_4'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(MaxPooling2D(pool_size=(2, 2))) # in 208, out 104
	model.add(Dropout(dropout))

	model.add(Convolution2D(64, 8, 8,init=weight_init, name='conv2_1'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(64, 6, 6,init=weight_init, name='conv2_2'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(64, 4, 4,init=weight_init, name='conv2_3'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(64, 2, 2,init=weight_init, name='conv2_4'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(MaxPooling2D(pool_size=(2, 2))) # in is 88, out is 44 
	model.add(Dropout(dropout))

	model.add(Flatten())
	model.add(Dense(220, init=weight_init))
	model.add(Activation('relu'))
	model.add(Dropout(dropout))

	model.add(Dense(2))
	model.add(Activation('sigmoid'))

	return model
Ejemplo n.º 10
0
def EVALUATE_IMAGE_seg_and_classify(h, w, file_i=1):
 #load test data
 read_path = '../Test_Images/testpkg3_unet/'
 ImageType = '.jpg'
 files = [s for s in os.listdir(read_path) if ImageType in s]

 # load seg model
 set_image_dim_ordering(dim_ordering='th')  # if not configured in
 model = load_model("../testbench/bg/" + bg_model)
 print 'loaded model', bg_model

 # debug
 # model.summary()

 for im_ori,im_i in zip(files,xrange(len(files))):
  im_ori = cv2.imread(read_path + im_ori)
  im_crop = im_ori
  image_crop_roi, screen_out = do_segment(model,im_ori,im_crop,im_i,h,w)
  cv2.imshow('seg_img_res',image_crop_roi)
  cv2.waitKey()
Ejemplo n.º 11
0
def cifar():

    # Determine proper input shape
    K.set_image_dim_ordering('th')
    input_shape = (1, 256, 192)
    img_input = Input(shape=input_shape)

    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_1')(img_input)
    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_2')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x)

    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_1')(x)
    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_2')(x)
    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(x)

    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_1')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_2')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(x)

    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_1')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_2')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(x)

    x = Flatten(name='flatten')(x)
    x = Dense(1000, activation='relu', name='fc1')(x)
    x = Dense(1000, activation='relu', name='fc2')(x)
    x = Dense(2, activation='softmax', name='pred')(x)

    # Create model.
    model = Model(img_input, x)

    #weights='MODS_keras_weights_3_he_normal_0.5_rmsprop_24.h5'
    #model.load_weights(weights)

    return model
Ejemplo n.º 12
0
import cPickle

from keras import backend as K
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.core import Dropout, Dense, Flatten
from keras.wrappers.scikit_learn import KerasClassifier

from sklearn.model_selection import GridSearchCV

import numpy as np

K.set_image_dim_ordering('th')  #input shape: (channels, height, width)

with open('cifar-10-batches-py/data_batch_1', 'r') as file:
    batch1 = cPickle.load(file)

with open('cifar-10-batches-py/data_batch_2', 'r') as file:
    batch2 = cPickle.load(file)

with open('cifar-10-batches-py/data_batch_3', 'r') as file:
    batch3 = cPickle.load(file)

with open('cifar-10-batches-py/data_batch_4', 'r') as file:
    batch4 = cPickle.load(file)

with open('cifar-10-batches-py/data_batch_5', 'r') as file:
    batch5 = cPickle.load(file)

Ejemplo n.º 13
0
def eva_image_6c(h, w):
 print 'start evaluation'
 images, mask = tools.import_data_unet_6c("../Test_Images/testpkg3_unet/", "cadbg_%03d.jpg", "fgbg_%03d.jpg", h, w, 3,
                                          do_Flipping=False)
 # params: loadData(data_path, file_img, file_mask, h, w, maxNum, do_Flipping=False)
 test_num = (images.shape)[0]
 print 'number of test images', test_num, ', shape', images.shape  # (3, 1, 320, 320)
 
 set_image_dim_ordering(dim_ordering='th')  # if not configured in
 
 try:
  model = load_model("../testbench/bg/" + bg_model)
  print 'loaded model', bg_model
 except Exception as e:
  print '[Hint-e]', e
 
 # debug
 # model.summary()
 
 ######################
 mean = np.mean(images)
 print 'mean', mean
 stdev = np.std(images)
 print 'stdev', stdev
 ######################
 
 images_original = images.copy()
 # print '3-number of test images', images_original.shape
 images = (images - mean) / stdev
 
 for i in range(0, images.shape[0]):
  start = time.time()
  ch = 3
  result = model.predict(images[i, :, :, :].reshape(1, ch, h, w), batch_size=1)  #
  end = time.time()
  print(end - start)
  print 'Test image', i + 1, ", Min/Max: %f %f" % (np.min(result), np.max(result))
  print 'result shape', (result.shape)
  res = result[0, 0, :, :].reshape((h, w)) * 255  # old
  
  print 'images_original shape', images_original.shape
  input_resized = cv2.resize(np.uint8(images_original[i, :, :, :].reshape(h, w, 3) * 255), (480, 360))
  # input_resized = cv2.resize(np.uint8(images_original[i, :, :, :].reshape(h, w) * 255), (480, 360)) #old
  # print 'min/max:%f %f' % (np.min(images_original[i, :, :, :]), np.max(images_original[i, :, :, :]))
  
  output_resized = cv2.resize(np.uint8(res), (480, 360))
  
  # input_rgb = cv2.cvtColor(input_resized, cv2.COLOR_GRAY2RGB)
  
  # cv2.imshow("input_rgb_window2", input_rgb)
  # cv2.waitKey(10000)
  
  # color = tools.add_colorOverlay(input_resized, output_resized)
  # combined = np.hstack((input_rgb, color))
  # cv2.imwrite('../testbench/res_' + bg_model[:-5] + '%03d.jpg' % i, np.uint8(res))
  
  # cv2.imwrite("../testbench/combined_%03d.jpg" % i, input_resized)
  # cv2.imwrite("../testbench/combined_%03d.jpg" % i, combined)
  # cv2.imwrite("../testbench/color_%03d.jpg" % i, color)
  
  # debug
  cv2.imshow("input_resized_window1", input_resized)
  cv2.imshow("out_window1", output_resized)
  cv2.imshow("res_window1", np.uint8(res))
  cv2.waitKey(500)
  
  ##########################################################
  # res2 = result[0,0, :, 1].reshape((320,320))*255
  # back = result[0,0, :, 2].reshape((320, 320)) * 255
  # cv2.imshow("Standing", np.uint8(res))
  # cv2.imshow("Sitting", np.uint8(res2))
  # cv2.imshow("Background", np.uint8(back))
  # cv2.imshow("input", np.uint8(((images[i, :, :, :] * stdev + mean) * 255).reshape(320, 320)))
  
  cv2.waitKey(30)
Ejemplo n.º 14
0
def EVALUATE_WEBCAM_seg_and_classify(camera_port, stop,num_class): #(cam)
  # load seg model
  set_image_dim_ordering(dim_ordering='th')  # if not configured in
  model = load_model("../testbench/bg/" + bg_model)
  print 'loaded model', bg_model

  n_classes = num_class

  #hy: check camera availability
  camera = cv2.VideoCapture(camera_port)

  frame_i = 0
  # hy: initialize confmatrix
  confMat2_TEST_Video = np.zeros((2, 2), dtype=np.float)

  #if stop == False:
  #if ckpt and ckpt.model_checkpoint_path:
    # Camera 0 is the integrated web cam

    # Number of frames to throw away while the camera adjusts to light levels
  #  ramp_frames = 1

  while True and not stop: #hy: confirm camera is available
      # Now we can initialize the camera capture object with the cv2.VideoCapture class.
      # All it needs is the index to a camera port.
      print 'Getting image...'

      ret, frame = camera.read()

      # Captures a single image from the camera and returns it in PIL format

      #ret = camera.set(3, 320) #hy use properties 3 and 4 to set frame resolution. 3- w, 4- h
      #ret = camera.set(4, 240)

      cv2.waitKey(1)
      # A nice feature of the imwrite method is that it will automatically choose the
      # correct format based on the file extension you provide.
      # cv2.imwrite(file, camera_capture)

      ####################################  /////////////////////////////


      if frame is not None:
        # print 'frame from webcam obtained'

        # hy: before continue check if image is read correctly
        # while frame is not None:
        frame_i += 1
        # hy:
        h_frame = frame.shape[0]
        w_frame = frame.shape[1]  # hy: h 1536 x w 2304
        print 'webcam frame shape h,w', h_frame,w_frame
      else:
        break

        # cv2.imshow("ori", frame)
      if frame_i % 50 == 0:
        use_focus_window = 0
        if use_focus_window:
         crop_x1 = 550  # 550
         crop_y1 = 700  # 700# 300
         area_step_size = 740  # 740# 640
         crop_x2 = crop_x1 + area_step_size * 1
         crop_y2 = crop_y1 + area_step_size * 1 * settings.h_resize / settings.w_resize
         frame_crop = frame[crop_y1:crop_y2, crop_x1:crop_x2]
         frame_crop_color = frame_crop.copy()
         # cv2.imwrite('../testbench/frame_color_tmp.jpg', np.uint8(frame_crop_color))
        else:
         crop_x1 = 0
         crop_y1 = 0

         crop_x2 = 0 + w_frame  # 2300  #1920
         crop_y2 = 0 + h_frame  # 1536  #1080
         frame_crop = frame[crop_y1:crop_y2, crop_x1:crop_x2]
         frame_crop_color = frame_crop.copy()


        frame_crop_roi, screen_out = do_segment(model, frame_crop_color, frame_i, 320, 320)


        do_classification(frame, frame_i, frame_crop_roi, confMat2_TEST_Video, crop_y1, crop_y2,
                          crop_x1, crop_x2, n_classes, screen_out)

        #TODO add termination condition

      print 'no frame retrieved'

  del(camera)
  return stop
Ejemplo n.º 15
0
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import pickle as pkl
import ISIC_dataset as ISIC
from metrics import dice_loss, jacc_loss, jacc_coef, dice_jacc_mean, jacc_coef_th, jacc_loss_th
import models
from sklearn.model_selection import train_test_split
from tqdm import tqdm, trange
from pre_processing import my_PreProc
from imgaug_test_cc import transform_img, reverse_gt
import argparse

np.random.seed(4)
K.set_image_dim_ordering('th')  # Theano dimension ordering: (channels, width, height)
                                # some changes will be necessary to run with tensorflow

# Folder of training images and masks
training_folder = "./datasets/ISIC2018_Task1-2_Training_Input"
training_mask_folder = "./datasets/ISIC2018_Task1_Training_GroundTruth"

# Folder of validation and test images
val_folder = "./datasets/ISIC2018_Task1-2_Validation_Input"
test_folder = "./datasets/ISIC2018_Task1-2_Test_Input"

# Folder to store predicted masks
val_predicted_folder = "./results/ISIC2018_Validation_Predicted"
test_predicted_folder = "./results/ISIC2018_Test_Predicted"

# Folder of external data
Ejemplo n.º 16
0
def foo():

    # Determine proper input shape
	if keras.__version__ > '1.0.3':
		K.set_image_dim_ordering('th')
	input_shape = (1, 224, 224)

	#img_input = Input(shape=input_shape)

	model = Sequential()

	model.add(Convolution2D(16, 3, 3,
			        input_shape=input_shape,init=weight_init, name='conv1_1'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(16, 3, 3,init=weight_init, name='conv1_2'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(MaxPooling2D(pool_size=(2, 2)))
	model.add(Dropout(dropout))

	model.add(Convolution2D(32, 3, 3,init=weight_init, name='conv2_1'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(32, 3, 3,init=weight_init, name='conv2_2'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(32, 3, 3,init=weight_init, name='conv2_3'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(MaxPooling2D(pool_size=(2, 2)))  
	model.add(Dropout(dropout))

	model.add(Convolution2D(64, 3, 3,init=weight_init, name='conv3_1'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(64, 3, 3,init=weight_init, name='conv3_2'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(MaxPooling2D(pool_size=(2, 2)))  
	model.add(Dropout(dropout))

	model.add(Convolution2D(128, 3,3,init=weight_init, border_mode='same', name='conv4_1'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Convolution2D(128, 3,3,init=weight_init, border_mode='same', name='conv4_2'))
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(MaxPooling2D(pool_size=(2, 2)))  
	model.add(Dropout(dropout))

	model.add(Flatten())
	model.add(Dense(120, init=weight_init))
	model.add(Activation('relu'))
	model.add(Dropout(dropout))

	model.add(Dropout(dropout))
	model.add(Dense(2))
	model.add(Activation('sigmoid'))

	return model
Ejemplo n.º 17
0
import numpy as np
import time
import os
import argparse
import h5py

from scipy.misc import imread, imresize, imsave
from scipy.optimize import fmin_l_bfgs_b

from sklearn.preprocessing import normalize

from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, ZeroPadding2D, AveragePooling2D
from keras import backend as Kr

Kr.set_image_dim_ordering('th')

# Command line arguments
parser = argparse.ArgumentParser(description='AI Artist')

parser.add_argument('--base_img_path',  metavar='base', type=str, help='Path to base image')
parser.add_argument('--style_img_path', metavar='ref',  type=str, help='Path to artistic style reference image')
parser.add_argument('--result_prefix',  metavar='res',  type=str, help='Prefix for saved results')

parser.add_argument('--rescale',        dest='rescale',        default='True',    type=str,   help='Rescale image after execution')
parser.add_argument('--keep_aspect',    dest='keep_aspect',    default='True',    type=str,   help='Maintain aspect ratio of image')
parser.add_argument('--tot_var_weight', dest='tv_weight',      default=1e-3,      type=float, help='Total variation in weights')
parser.add_argument('--content_weight', dest='content_weight', default=0.025,     type=float, help='Weight of content')
parser.add_argument('--style_weight',   dest='style_weight',   default=1,         type=float, help='Weight of style')
parser.add_argument('--img_size',       dest='img_size',       default=512,       type=int,   help='Output image size')
parser.add_argument('--content_layer',  dest='content_layer',  default='conv5_2', type=str,   help="Optional: 'conv4_2'")
Ejemplo n.º 18
0
def VGG_16(classes_number):
    from keras.models import Sequential
    from keras.layers.core import Dense, Dropout, Flatten
    from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D
    from keras import backend as K

    K.set_image_dim_ordering('th')

    # VGG16: https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3
    model = Sequential()
    model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3)))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(128, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(128, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(256, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(256, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(256, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))

    f = h5py.File(WEIGHTS_PATH + 'vgg16_weights.h5')
    for k in range(f.attrs['nb_layers']):
        if k >= len(model.layers):
            # we don't look at the last (fully-connected) layers in the savefile
            break
        g = f['layer_{}'.format(k)]
        weights = [
            g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])
        ]
        model.layers[k].set_weights(weights)
    f.close()
    print('Model loaded.')

    model.add(Dense(classes_number, activation='sigmoid'))
    return model
Ejemplo n.º 19
0
def get_model2(input_shape=input_shape_resnet, num_classes=1):
    K.set_image_data_format('channels_last')
    linknet = LinkNet2(input_shape=input_shape_resnet)
    K.set_image_dim_ordering('tf')

    #inputs = linknet.get_input().output
    #inputs.set_shape( ( None ,224,224 , 3 ))
    inputs = linknet.get_input()

    print(inputs.shape)
    #inputs = Input(shape = input_shape_resnet )
    # lines to work with de padding layer
    x = linknet.firstpad(inputs)
    x = linknet.firstconv(x)

    #x = linknet.firstconv( inputs )
    print("xxxxx shape")
    print(x.shape)
    x = linknet.firstbn(x)
    x = linknet.firstrelu(x)
    x = linknet.firstmaxpool(x)
    print("input shape encoders")
    print(x.shape)
    e1 = linknet.encoder1.call2(x)
    e2 = linknet.encoder2.call2(e1)
    e3 = linknet.encoder3.call2(e2)

    e4 = linknet.encoder4.call2(e3)

    print("encoders shapes ")
    print(e1.shape)
    print(e2.shape)
    print(e3.shape)
    print(e4.shape)

    #d4 = linknet.decoder4( e4 )

    #d4 = linknet.decoder4.call2( e4 ) + e3
    #print( "e2 upsampled ")
    #print(e2.shape)

    d4 = linknet.decoder4.call2(e4)
    e3 = UpSampling2D((2, 2))(e3)
    d4 = keras.layers.Add()([e3, d4])

    #d3 = linknet.decoder3.call2( d4 ) + e2
    d3 = linknet.decoder3.call2(d4)

    #y = UpSampling2D((2,2) )( e2 )
    x = UpSampling2D((2, 2))(e2)
    x = UpSampling2D((2, 2))(x)
    d3 = keras.layers.Add()([x, d3])
    #d2 = linknet.decoder2.call2( d3 )  + e1

    d2 = linknet.decoder2.call2(d3)

    y = UpSampling2D((8, 8))(e1)
    d2 = keras.layers.Add()([y, d2])  # 440 , 440 , 64
    print("dedede")
    print(d2.shape)
    d1 = linknet.decoder1.call2(d2)
    print("decoders shape")

    print(d4.shape)
    print(d3.shape)
    print(d2.shape)
    print(d1.shape)

    y = Conv2D(32, kernel_size=(3, 3), strides=2, padding="same")(d1)
    y = BatchNormalization()(y)
    y = Activation("relu")(y)

    # [ None , 440 , 440 , 32]

    y = Conv2D(16, kernel_size=(2, 2), strides=2, padding="same")(y)
    y = BatchNormalization()(y)
    y = Activation("relu")(y)

    # Output [ 220 , 220 , 16]

    output_final = Conv2D(1, kernel_size=(1, 1), activation="sigmoid")(y)
    """
	#y = Conv2D( 8 , kernel_size=(2,2) , strides = 2 , padding= "same")(y)
	#y = BatchNormalization( )(y)
	#y = Activation("relu")(y)

	# [110 , 110 , 8]
	y = Conv2D( 4 , kernel_size=(2,2) , strides = 2 , padding="same")(y)
	y = BatchNormalization( )(y)
	y = Activation("relu")(y)

	# {55 , 55 , 4} 
	#y.set_shape( (2,55,55,4 ) )

	y = Conv2D( 4 , kernel_size=(5,5) , strides = 2 )(y)
	y = BatchNormalization( )(y)
	y = Activation("relu")(y)

	#y = Conv2D( 2 , kernel_size=(5,5) , strides = 2 )(y)
	#y = BatchNormalization( )(y)
	#y = Activation("relu")(y)

	# ( 13 , 13 , 2 )
	rs = Lambda(  lambda x : x  , output_shape = (2,26,26,4) )(y) 
	#rs = Reshape(   [55*55*4]  )( y )
	rs = Flatten( ) (y )
	size = 224*224
	#flat = Flatten(  )( y )
	fc = Dense( size   )(  rs  )
	fc = Activation("relu")(fc)
	print("asdadasda")
	print( fc.shape )
	#utput = Reshape( ( -1 , ) )(fc)
	#fc.set_shape( ( None , 224 ,224 , 1  ) )
	print("output")

	#print(output)

	# [None , 220 , 220 , 32]
	#f1 = linknet.finaldeconv1( d1 )
	#f2 = Activation("relu")( f1 )
	#f3 = linknet.finalconv2( f2 )
	#f4 = linknet.finalrelu2( f3)
	#f5 = linknet.finalconv3( f4 )

	#print( f1.shape )
	#f5.set_shape( (None , 224 , 224 , 1 ))
	"""
    model = Model(inputs=inputs, outputs=output_final)

    print(model.summary())
    return model
Ejemplo n.º 20
0
import sys
sys.path.append('..') # this allows it to find data_loader
sys.setrecursionlimit(90000)
from keras.models import Sequential,Model
from keras.layers import Activation,Convolution2D,LocallyConnected2D,Flatten,Dense,Input,Reshape,Embedding,Merge,merge
from keras.layers.convolutional import UpSampling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.normalization import BatchNormalization
from keras.layers.pooling import AveragePooling2D
from keras.callbacks import CSVLogger,EarlyStopping,ModelCheckpoint
import keras.backend as K
K.set_image_dim_ordering('th') # NOTE: channel is first dim after batch.

import data_loader
import train

def small_discriminator(args):
    DIMS = (args['cropped_width']*args['cropped_width'], data_loader.DATA_DIM[2])
    
    d_input =  Input(shape=(1, DIMS[0], DIMS[1]), name='d_input')
    d_input_particle = Input(shape=(1,), name='d_input_particle')
    d_input_energy = Input(shape=(1,), name='d_input_energy')
    
    hid = Convolution2D(32, 5, 5, border_mode='same')(d_input)
    hid = Flatten()(hid)
    hid = merge([hid, d_input_particle, d_input_energy], mode='concat')
#    hid = merge([hid, d_input_energy], mode='concat')

    hid = Dense(8, activation='relu')(hid)
    
    d_proba = Dense(1, activation='linear', name='d_proba')(hid)
Ejemplo n.º 21
0
def YOLO(input_shape=(3, 416, 416), num_classes=80, num_priors=5):
    """YOLO (v2) architecture

    # Arguments
        input_shape: Shape of the input image
        num_classes: Number of classes (excluding background)

    # References
        https://arxiv.org/abs/1612.08242
        https://arxiv.org/abs/1506.02640
    """
    K.set_image_dim_ordering('th')

    net = {}
    input_tensor = Input(shape=input_shape)
    net['input'] = input_tensor
    net['conv1'] = (YOLOConvolution2D(32,
                                      3,
                                      3,
                                      border_mode='same',
                                      subsample=(1, 1),
                                      epsilon=0.000001))(net['input'])
    net['relu1'] = (LeakyReLU(alpha=0.1))(net['conv1'])
    net['pool1'] = (MaxPooling2D(pool_size=(2, 2),
                                 border_mode='valid'))(net['relu1'])
    net['conv2'] = (YOLOConvolution2D(64,
                                      3,
                                      3,
                                      border_mode='same',
                                      subsample=(1, 1),
                                      epsilon=0.000001))(net['pool1'])
    net['relu2'] = (LeakyReLU(alpha=0.1))(net['conv2'])
    net['pool2'] = (MaxPooling2D(pool_size=(2, 2),
                                 border_mode='valid'))(net['relu2'])
    net['conv3_1'] = (YOLOConvolution2D(128,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['pool2'])
    net['relu3_1'] = (LeakyReLU(alpha=0.1))(net['conv3_1'])
    net['conv3_2'] = (YOLOConvolution2D(64,
                                        1,
                                        1,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu3_1'])
    net['relu3_2'] = (LeakyReLU(alpha=0.1))(net['conv3_2'])
    net['conv3_3'] = (YOLOConvolution2D(128,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu3_2'])
    net['relu3_3'] = (LeakyReLU(alpha=0.1))(net['conv3_3'])
    net['pool3'] = (MaxPooling2D(pool_size=(2, 2),
                                 border_mode='valid'))(net['relu3_3'])
    net['conv4_1'] = (YOLOConvolution2D(256,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['pool3'])
    net['relu4_1'] = (LeakyReLU(alpha=0.1))(net['conv4_1'])
    net['conv4_2'] = (YOLOConvolution2D(128,
                                        1,
                                        1,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu4_1'])
    net['relu4_2'] = (LeakyReLU(alpha=0.1))(net['conv4_2'])
    net['conv4_3'] = (YOLOConvolution2D(256,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu4_2'])
    net['relu4_3'] = (LeakyReLU(alpha=0.1))(net['conv4_3'])
    net['pool4'] = (MaxPooling2D(pool_size=(2, 2),
                                 border_mode='valid'))(net['relu4_3'])
    net['conv5_1'] = (YOLOConvolution2D(512,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['pool4'])
    net['relu5_1'] = (LeakyReLU(alpha=0.1))(net['conv5_1'])
    net['conv5_2'] = (YOLOConvolution2D(256,
                                        1,
                                        1,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu5_1'])
    net['relu5_2'] = (LeakyReLU(alpha=0.1))(net['conv5_2'])
    net['conv5_3'] = (YOLOConvolution2D(512,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu5_2'])
    net['relu5_3'] = (LeakyReLU(alpha=0.1))(net['conv5_3'])
    net['conv5_4'] = (YOLOConvolution2D(256,
                                        1,
                                        1,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu5_3'])
    net['relu5_4'] = (LeakyReLU(alpha=0.1))(net['conv5_4'])
    net['conv5_5'] = (YOLOConvolution2D(512,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu5_4'])
    net['relu5_5'] = (LeakyReLU(alpha=0.1))(net['conv5_5'])
    net['pool5'] = (MaxPooling2D(pool_size=(2, 2),
                                 border_mode='valid'))(net['relu5_5'])
    net['conv6_1'] = (YOLOConvolution2D(1024,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['pool5'])
    net['relu6_1'] = (LeakyReLU(alpha=0.1))(net['conv6_1'])
    net['conv6_2'] = (YOLOConvolution2D(512,
                                        1,
                                        1,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu6_1'])
    net['relu6_2'] = (LeakyReLU(alpha=0.1))(net['conv6_2'])
    net['conv6_3'] = (YOLOConvolution2D(1024,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu6_2'])
    net['relu6_3'] = (LeakyReLU(alpha=0.1))(net['conv6_3'])
    net['conv6_4'] = (YOLOConvolution2D(512,
                                        1,
                                        1,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu6_3'])
    net['relu6_4'] = (LeakyReLU(alpha=0.1))(net['conv6_4'])
    net['conv6_5'] = (YOLOConvolution2D(1024,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu6_4'])
    net['relu6_5'] = (LeakyReLU(alpha=0.1))(net['conv6_5'])
    net['conv6_6'] = (YOLOConvolution2D(1024,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu6_5'])
    net['relu6_6'] = (LeakyReLU(alpha=0.1))(net['conv6_6'])
    net['conv6_7'] = (YOLOConvolution2D(1024,
                                        3,
                                        3,
                                        border_mode='same',
                                        subsample=(1, 1),
                                        epsilon=0.000001))(net['relu6_6'])
    net['relu6_7'] = (LeakyReLU(alpha=0.1))(net['conv6_7'])
    net['reorg7'] = (Reorg())(net['relu5_5'])
    net['merge7'] = (merge([net['reorg7'], net['relu6_7']],
                           mode='concat',
                           concat_axis=1))
    net['conv8'] = (YOLOConvolution2D(1024,
                                      3,
                                      3,
                                      border_mode='same',
                                      subsample=(1, 1),
                                      epsilon=0.000001))(net['merge7'])
    net['relu8'] = (LeakyReLU(alpha=0.1))(net['conv8'])
    net['conv9'] = (Convolution2D(num_priors * (4 + num_classes + 1),
                                  1,
                                  1,
                                  border_mode='same',
                                  subsample=(1, 1)))(net['relu8'])

    model = Model(net['input'], net['conv9'])
    return model
Ejemplo n.º 22
0
Refernce:
https://machinelearningmastery.com/handwritten-digit-recognition-using-convolutional-neural-networks-python-keras/
@author: jkuo
"""

import numpy as np
np.random.seed(123)  # for reproducibility

from keras.models import Sequential  # linear stack of NN layers
from keras.layers import Dense, Dropout, Activation, Flatten  # core layers from Keras
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils  #utils used to transform data
from keras import backend as K
K.set_image_dim_ordering(
    'th')  #using the theano ordering where the color channel comes first
#keras.backend.backed()# to check backend: tensorflow

from keras.datasets import mnist  #mnist dataset

from matplotlib import pyplot as plt

# Load pre-shuffled MNIST data into train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
#print(X_train.shape) #(60000, 28, 28) 28x28 pixel images

# plot 4 images as gray scale
plt.subplot(221)
plt.imshow(X_train[0], cmap=plt.get_cmap('gray'))
plt.subplot(222)
plt.imshow(X_train[1], cmap=plt.get_cmap('gray'))
Ejemplo n.º 23
0
mpl.use('Agg')  # no display mode
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
from argparse import ArgumentParser
from datetime import datetime
from keras.applications.mobilenet import MobileNet
from keras.optimizers import TFOptimizer
from keras import backend as K  # backend of Keras
from keras.utils import multi_gpu_model, to_categorical
from keras.callbacks import ModelCheckpoint
from multiprocessing import cpu_count
import scipy.misc

np.random.seed(1)
K.set_image_dim_ordering('tf')  # use Tensorflow backend

# parameters of learning for the network
EPOCHES = 10  # how many times the whole train set will be shown for model
BATCH_SIZE = 128  # count of train samples, which are shown to optimizer before updating weights of network
IMG_ROWS, IMG_COLS = 224, 224  # size of images (at least 32x32)
IMG_CHANNELS = 1  # colour channels of images
CLASSES = 1000  # count of classes

# folders for savings
RESULTS_DIR = '../results'
WEIGHTS_DIR = '../weights'

# parameters of optimizer
LEARNING_RATE = 0.01
DECAY = 0.9
Ejemplo n.º 24
0
@author: Supriya
"""

# Import all the things we need ---
#   by setting env variables before Keras import you can set up which backend and which GPU it uses
%matplotlib inline
import os,random
os.environ["KERAS_BACKEND"] = "theano"
# os.environ["KERAS_BACKEND"] = "tensorflow"
os.environ["THEANO_FLAGS"]  = "floatX=float32,device=cpu,nvcc.flags=-D_FORCE_INLINES"
import numpy as np
import theano as th
import theano.tensor as T
from keras import backend as kBack
kBack.set_image_dim_ordering('th')
from keras.utils import np_utils
import keras.models as models
from keras.layers.core import Reshape,Dense,Dropout,Activation,Flatten
from keras.layers.noise import GaussianNoise
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.regularizers import *
from keras.optimizers import adam
import matplotlib.pyplot as plt
import seaborn as sns
import pickle, random, sys, keras
from collections import defaultdict
def create_model():
    dr = 0.5 # dropout rate (%)
    model = models.Sequential()
    model.add(Reshape([1]+in_shp, input_shape=in_shp))
def train_single_classification_model_full_frame(num_fold, train_files, valid_files, restore, optim_name='Adam'):
    from keras.callbacks import EarlyStopping, ModelCheckpoint, CSVLogger
    from keras.optimizers import Adam, SGD
    import keras.backend as K

    K.set_image_dim_ordering('th')
    cnn_type = 'VGG_3D_24_56_56_v2'
    print('Creating and compiling model [{}]...'.format(cnn_type))
    model = VGG_3D_24_56_56(24)

    final_model_path = MODELS_PATH + '{}_fold_{}.h5'.format(cnn_type, num_fold)
    cache_model_path = MODELS_PATH + '{}_temp_fold_{}.h5'.format(cnn_type, num_fold)
    cache_model_path_detailed = MODELS_PATH + '{}_temp_fold_{}_'.format(cnn_type, num_fold) + 'ep_{epoch:03d}_loss_{val_loss:.4f}.h5'
    if os.path.isfile(cache_model_path) and restore:
        print('Load model from last point: ', cache_model_path)
        model.load_weights(cache_model_path)
    else:
        print('Start training from begining')

    print('Fitting model...')

    batch_size = 32
    learning_rate = 0.0001
    epochs = 2000
    patience = 50
    print('Batch size: {}'.format(batch_size))
    print('Model memory usage: {} GB'.format(get_model_memory_usage(batch_size, model)))
    print('Learning rate: {}'.format(learning_rate))
    steps_per_epoch = 200
    validation_steps = 200
    print('Samples train: {}, Samples valid: {}'.format(steps_per_epoch, validation_steps))

    if optim_name == 'SGD':
        optim = SGD(lr=learning_rate, decay=1e-6, momentum=0.9, nesterov=True)
    else:
        optim = Adam(lr=learning_rate)
    model.compile(optimizer=optim, loss='binary_crossentropy', metrics=['accuracy'])

    callbacks = [
        EarlyStopping(monitor='val_loss', patience=patience, verbose=0),
        ModelCheckpoint(cache_model_path, monitor='val_loss', save_best_only=True, verbose=0),
        ModelCheckpoint(cache_model_path_detailed, monitor='val_loss', save_best_only=False, save_weights_only=True, verbose=0),
        CSVLogger(HISTORY_FOLDER_PATH + 'history_fold_{}_{}_lr_{}_optim_{}.csv'.format(num_fold, cnn_type, learning_rate, optim_name), append=True)
    ]

    history = model.fit_generator(generator=batch_generator_train(train_files, batch_size),
                  epochs=epochs,
                  steps_per_epoch=steps_per_epoch,
                  validation_data=batch_generator_train(valid_files, batch_size),
                  validation_steps=validation_steps,
                  verbose=2,
                  max_queue_size=16,
                  callbacks=callbacks)

    min_loss = min(history.history['val_loss'])
    print('Minimum loss for given fold: ', min_loss)
    model.load_weights(cache_model_path)
    model.save(final_model_path)
    now = datetime.datetime.now()
    filename = HISTORY_FOLDER_PATH + 'history_{}_{}_{:.4f}_lr_{}_{}.csv'.format(cnn_type, num_fold, min_loss, learning_rate, now.strftime("%Y-%m-%d-%H-%M"))
    pd.DataFrame(history.history).to_csv(filename, index=False)
    return min_loss
Ejemplo n.º 26
0
import os

# Third Party
import numpy as np
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras import backend as K
from keras.optimizers import Adam
from keras.models import Model
from keras.layers import Input
import nibabel as nb

# Internal
import dvpy as dv
import dvpy.tf

K.set_image_dim_ordering('tf')  # Tensorflow dimension ordering in this code


class config:
    def __init__(self):
        # Dimension of padded input, for training.
        self.dim = (256, 256)

        # Number of Classes (Including Background)
        self.num_classes = 6

        # How many images should be processed in each batch?
        self.batch_size = 32

        # UNet Depth
        self.unet_depth = 5
Ejemplo n.º 27
0
def EVA_IMAGE_seg_and_classify(MODEL_ID, bg_model, h, w,
                               in_ch=1, best_avg=0, save_res_path='', save_imgs=False, file_i=1, with_gt=False,
                               step_show=False, stop=False):
  print 'Model ID:', MODEL_ID

  res_pass_list, res_fail_list, list_of_imgs_res = [], [], []
  dices, dice_non_empty, dice_empty, dices_cad, count_test, n_classes = 0, 0, 0, 0, 0, 2
  max_dice, min_dice, avg_dice_non_empty, avg_dice_empty, overall_acc = 0, 1, 0, 0, 0
  confMat1_TEST = np.zeros((n_classes, n_classes), dtype=np.float)  # hy collect detailed confusion matrix
  confMat2_TEST = np.zeros((2, 2), dtype=np.float)

  read_paths_im, read_paths_m, files_im, files_m = get_read_path_and_files_for_im_m_2cl(read_from_file=False)
  # load seg model

  set_image_dim_ordering(dim_ordering='th')  #
  model = load_model(bg_model)

  if INFO_0:
    print 'loading model', bg_model

  # model.summary()
  if result_for_table:
    print 'True/False', 'No.', 'Name', 'TargetLabel', 'PredictLabel', 'Precision', \
 \
      'Top2', 'Top2_pres', 'Top3', 'Top3_pres', 'Top4', 'Top4_pres', 'Top5', 'Top5_pres', 'last', 'last_pres'


  cust_list = ['']  # can define any filename patterns
  for im_ori, ref_mask, i, read_path_im, read_path_m in zip(files_im, files_m, xrange(len(files_im)), read_paths_im,
                                                            read_paths_m):
  
    fn = os.path.basename(im_ori)[:-4]
  
    if i > -1:  # > 46 and i < 54:
      # if fn in cust_list: # 0 and i < 4:#> 46 and i < 54:
      if INFO_0:
        print 'read path:', read_path_im
  
      im_ori_view = cv2.imread(read_path_im + im_ori)
      im_crop = im_ori_view.copy()
      im_ori_view = cv2.resize(im_ori_view, (h, w))
  
      # IMAGE
      pred_int, pred_thresh,fr_add_cont, old_mask,old_roi_res, \
      new_mask, image_crop_roi, r1, r2, c1, c2, screen_out, roi_whitebg \
        = do_segment_im(model, im_ori_view, im_crop, i, h, w, in_ch, show=False, save=False)
  
    pred_thresh = new_mask.copy()  # use optimized mask for dice calc
  
    if len(pred_thresh.shape) == 3:
      pred_thresh = cv2.cvtColor(pred_thresh, cv2.COLOR_RGB2GRAY)
  
    if with_gt:
      ref_mask = cv2.resize(cv2.imread(read_path_m + ref_mask, 0), (h, w))
      ref_mask_thresh = ref_mask  # * 255.0
      thresh_ref = 0
      ref_key = 255
  
      idx = ref_mask_thresh[:, :] > thresh_ref
      ref_mask_thresh[idx] = ref_key
      pred_key = 255
  
      if search_str not in fn:
        count_test += 1
        dice = tools.calc_dice_simi(pred_thresh, ref_mask_thresh, fn, k=pred_key)
        print '\nDice for:', fn, ':', dice
        dices += dice
  
        if dice < min_dice:
          min_dice = dice
  
        if dice > max_dice:
          max_dice = dice
  
        if do_classification:
          target_label = tools.get_ground_truth_label_im(read_path_im)
          total_imgs_of_class = len(os.listdir(read_path_im))
  
          if target_label == 1:
            dice_non_empty += dice
  
            avg_dice_non_empty = dice_non_empty / total_imgs_of_class
  
          if target_label == 0:
            dice_empty += dice
            avg_dice_empty = dice_empty / total_imgs_of_class
  
    prefix = get_model_index(bg_model, '\d')
    pre_tensor_view = np.zeros((h, w, 3), np.uint8)
  
    if screen_out:
      classified_as = 1
  
      if DEBUG:
        print 'prefix:', prefix
  
      found_dig = re.search('\d', os.path.basename(bg_model))
      if found_dig:
        dig = found_dig.start()
        prefix = save_res_path + os.path.basename(bg_model)[dig:-5]
      else:
        print 'no file found'
      reduce_border = False
      if reduce_border:
        # image_crop_roi = cv2.resize(im_crop,(h,w))[r1+border:r2-border, c1-border:c2+border] #crop_y1:crop_y2, crop_x1:crop_x2
        image_crop_roi = cv2.resize(im_crop, (h, w))[r1 + border:r2 - border,
                         c1 + border:c2 - border]  # crop_y1:crop_y2, crop_x1:crop_x2
  
      if INFO_0:
        print 'image_crop_roi shape (tf ordering hwc):', image_crop_roi.shape  # tf ordering channel last
  
      if min(image_crop_roi.shape) < 1:
        # demo_final_seg_result(fn,pred_thresh,fr_add_cont,image_crop_roi,prefix,screen_out,save_im=False,save_stack_imgs=False)
  
        print 'tensor size too small'
  
        cv2.putText(fr_add_cont, 'too small ROI', org=(10, 20), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale=1,
                    color=(0, 255, 0), thickness=2)
        pre_tensor_view = np.zeros((h, w, 3), np.uint8)
  
    else:
      print 'no FG'
      classified_as = 0
      cv2.putText(fr_add_cont, 'no FG', org=(10, 20), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1,
                  color=(0, 255, 0), thickness=2)
      pre_tensor_view = np.zeros((h, w, 3), np.uint8)
  
    if do_classification:
      overall_acc, confMat1_TEST \
        = EVA_IMAGE_classify_seats(read_path_im, im_ori_view, im_crop, pred_thresh,
                                   classified_as, count_test, confMat1_TEST, confMat2_TEST, border,
                                   i, n_classes, fn)
  
    if create_stacked_imgs_for_paper:
      s_size = (320, 320)
      if read_path_im == read_path_m:
        list_of_imgs = [cv2.resize(im_ori_view, s_size), cv2.resize(pred_int, s_size),
                        cv2.resize(new_mask, s_size),
                        ]
    
        winname = 'in-pred-oldmask-newmask'
      else:
        # list_of_imgs = [cv2.resize(im_ori_view, (320 / 2, 320 / 2)), cv2.resize(pred_int, s_size),
        #               cv2.resize(old_mask, (320 / 2, 320 / 2)), cv2.resize(fr_add_cont, s_size)]
        list_of_imgs = [cv2.resize(im_ori_view, s_size), cv2.resize(ref_mask, s_size), cv2.resize(pred_int, s_size),
                        cv2.resize(new_mask, s_size),
                        cv2.resize(fr_add_cont, s_size),
                        cv2.resize(roi_whitebg, s_size), cv2.resize(pre_tensor_view, s_size)]
    
        winname = 'in-gt-pred-newmask-cont-pretensor'
    
      print 'save path:', prefix, fn
    
      stacked_imgs = demo_stacked_n_col_images(prefix, fn, list_of_imgs, winname, save_im=True)
      list_of_imgs_res.append(stacked_imgs)
    
    # cv2.imwrite(save_res_path + 'stack_'+ os.path.basename(files_im[i]), stacked_imgs)
    
    
    if step_show and not CLOSE_ALL and not stop:
      k = cv2.waitKey(30) & 0xFF
      while True and not stop:
        if k == ord('n'):
          print 'add to fail_list:', fn
    
          res_fail_list.append(read_path_im + os.path.basename(files_im[i]))
          # res_fail_list.append(files[i])
          break
    
        elif k == ord('y'):
          print 'add to pass_list:', fn
          res_pass_list.append(read_path_im + os.path.basename(files_im[i]))
          save_imgs = False
    
          if save_imgs:
            cv2.imwrite(save_res_path + 'stack_' + os.path.basename(files_im[i]), stacked_imgs)
          break
    
        elif k == ord('q'):  # ESC
          stop = True
          break
    
        else:
          k = cv2.waitKey(30) & 0xFF
          if k != 255:
            print 'k:', k  # 81-l, 83-r, 82-u, 84-d
      if cv2.waitKey(1) & 0xFF == ord('q'):
        print 'key interrupt'
        break
    
  print_res_list = False
  
  if print_res_list:  #
    print bg_model, 'test package:', read_path_im
    print 'res_fail_list=', res_fail_list, '\nres_pass_list=', res_pass_list
    print 'num of fail:', len(res_fail_list), '\nnum of pass:'******'\n'.join(lines)
    with open(file, 'w') as f:
      f.writelines(lines)
  
  
  save_eva_to_file = False
  
  if save_eva_to_file:
    keyword = os.path.basename(os.path.normpath(read_path_im))
  
    save_to_file('../Test_Images/img_list/fail_' + keyword + '_' + os.path.basename(bg_model)[:-5] + '.txt',
                 res_fail_list)
  
    save_to_file('../Test_Images/img_list/pass_' + keyword + '_' + os.path.basename(bg_model)[:-5] + '.txt',
                 res_pass_list)
  
  cv2.destroyAllWindows()
  
  if count_test > 0:
    avg_dice = float(dices / count_test)
  else:
    avg_dice = 0
  
  if avg_dice > best_avg:
    best_avg = avg_dice
  
  print '\nseg avg dice:', avg_dice, 'max dice:', max_dice, ', min dice:', min_dice, ', ', bg_model
  
  if do_classification:
    print '2classifier overall_acc', overall_acc
    print tools.print_label_title()
    print '#######'
    print confMat1_TEST
    print 'seg avg_non_empty:', avg_dice_non_empty, 'seg avg_empty:', avg_dice_empty
  
  else:
    overall_acc = 0
  num_total_ims = len(list_of_imgs_res)
  print 'list imgs res:', num_total_ims
  
  # create_stacked_imgs_for_paper = True
  if create_stacked_imgs_for_paper and num_total_ims > 0 and num_total_ims < 7:
    MODEL_ID = ''
    demo_stacked_n_row_images(prefix, MODEL_ID, list_of_imgs_res, winname, save_im=True)
  
  return best_avg, bg_model, overall_acc, stop
Ejemplo n.º 28
0
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.applications.resnet50  import ResNet50
from keras.applications.resnet50  import preprocess_input as r_preprocess
from keras.layers import Dropout, Input, Lambda, Convolution2D, MaxPooling2D, Flatten, Dense, AveragePooling2D
from keras.engine.topology import Layer
from keras.models import Model
from keras.optimizers import Adam, SGD
from keras.callbacks import ModelCheckpoint, Callback
from keras import backend as K
from keras import regularizers
from load_data import load_caption

img_dim_ordering = 'tf'
K.set_image_dim_ordering(img_dim_ordering)
tf.reset_default_graph()


#Lexical FCN model with MIML loss
def pretrained_model(img_shape, length):
    '''
    img_shape = (320, 320, 3)
    length = 7677
    '''
    keras_input = Input(shape=img_shape, name = 'image_input')        
    model_resnet50_conv = ResNet50(input_shape=img_shape, weights='imagenet', include_top=False)
    #model_vgg16_conv.summary()
    x = model_resnet50_conv(keras_input)#(batch, 10, 10, 512)
    print(model_resnet50_conv.summary())
    x = AveragePooling2D(pool_size = (3, 3), strides = (3, 3), padding = 'same',  name='avg_pool')(x)
Ejemplo n.º 29
0
		return model

# network and training
NB_EPOCH = 20
BATCH_SIZE = 128
VERBOSE = 1
OPTIMIZER = Adam()
VALIDATION_SPLIT=0.2

IMG_ROWS, IMG_COLS = 28, 28 # input image dimensions
NB_CLASSES = 10  # number of outputs = number of digits
INPUT_SHAPE = (1, IMG_ROWS, IMG_COLS)

# data: shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
K.set_image_dim_ordering("th")

# consider them as float and normalize
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255 
X_test /= 255  

# we need a 60K x [1 x 28 x 28] shape as input to the CONVNET
X_train = X_train[:, np.newaxis, :, :]
X_test = X_test[:, np.newaxis, :, :]

print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Ejemplo n.º 30
0
import numpy as np
import pickle
import cv2
import os
from keras import optimizers  #for compiling a keras model
from keras.models import Sequential  #linear stack of layers
from keras.layers import Dense  #Dense is a name for fully connected/linear layer in keras
from keras.layers import Dropout  #Dropout is a technique where randomly selected neurons are ignored during training.
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint  #Adding checkpoints to your trainning process
from keras import backend as K  #Keras does not do simple level operations like convolution so tensorflow or theano does it
K.set_image_dim_ordering(
    'tf'
)  #"tf" format means that the convolutional kernels will have the shape (rows, cols, input_depth, depth)


def get_image_size():
    img = cv2.imread('gestures/0/100.jpg', 0)
    return img.shape


def get_num_of_classes():
    return len(os.listdir('gestures/'))


image_x, image_y = get_image_size()

Ejemplo n.º 31
0
def get_unet(img_rows, img_cols):
    K.set_image_dim_ordering('th')
    inputs = Input((1, img_rows, img_cols))
    conv1 = Convolution2D(32, 3, 3, activation='relu',
                          border_mode='same')(inputs)
    conv1 = Convolution2D(32, 3, 3, activation='relu',
                          border_mode='same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = Convolution2D(64, 3, 3, activation='relu',
                          border_mode='same')(pool1)
    conv2 = Convolution2D(64, 3, 3, activation='relu',
                          border_mode='same')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(pool2)
    conv3 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(pool3)
    conv4 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Convolution2D(512, 3, 3, activation='relu',
                          border_mode='same')(pool4)
    conv5 = Convolution2D(512, 3, 3, activation='relu',
                          border_mode='same')(conv5)

    up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4],
                mode='concat',
                concat_axis=1)
    conv6 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(up6)
    conv6 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(conv6)

    up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3],
                mode='concat',
                concat_axis=1)
    conv7 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(up7)
    conv7 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(conv7)

    up8 = merge([UpSampling2D(size=(2, 2))(conv7), conv2],
                mode='concat',
                concat_axis=1)
    conv8 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(up8)
    conv8 = Convolution2D(64, 3, 3, activation='relu',
                          border_mode='same')(conv8)

    up9 = merge([UpSampling2D(size=(2, 2))(conv8), conv1],
                mode='concat',
                concat_axis=1)
    conv9 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(up9)
    conv9 = Convolution2D(32, 3, 3, activation='relu',
                          border_mode='same')(conv9)

    conv10 = Convolution2D(1, 1, 1, activation='sigmoid')(conv9)

    model = Model(input=inputs, output=conv10)

    model.compile(optimizer=Adam(lr=1.0e-5),
                  loss=dice_coef_loss,
                  metrics=[dice_coef])

    return model
Ejemplo n.º 32
0
print()
print('Begin Deep Learning Process (Simple Deep Learning')

NB_EPOCH = 150
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10
OPTIMIZER = Adam()
N_HIDDEN = 128
VALIDATION_SPLIT = 0.2
RESHAPE = 784
DROPOUT = 0.2
IMG_ROW, IMG_COL = 28, 28
INPUT_SHAPE = (1, IMG_ROW, IMG_COL)

K.set_image_dim_ordering("th")
X_train = X_train.reshape(60000, 1, IMG_ROW, IMG_COL)
X_test = X_test.reshape(20000, 1, IMG_ROW, IMG_COL)

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

X_train /= 255
X_test /= 255

print('{} Train Samples'.format(X_train.shape[0]))
print('{} Test Samples'.format(X_test.shape[0]))

Y_train = np_utils.to_categorical(Y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(Y_test, NB_CLASSES)
Ejemplo n.º 33
0
def EVALUATE_VIDEO_seg_and_classify(VIDEO_FILE, num_class):  # (v)
 # load seg model
 set_image_dim_ordering(dim_ordering='th')  # if not configured in
 model = load_model("../testbench/bg/" + bg_model)
 print 'loaded model', bg_model
 
 n_classes = num_class
 video = cv2.VideoCapture(VIDEO_FILE)  # hy: changed from cv2.VideoCapture()
 
 video.set(1, 2)  # hy: changed from 1,2000 which was for wheelchair test video,
 # hy: propID=1 means 0-based index of the frame to be decoded/captured next
 
 if not video.isOpened():
  print "cannot find or open video file"
  exit(-1)
 
 # hy: initialize confmatrix
 confMat2_TEST_Video = np.zeros((2, 2), dtype=np.float)
 
 ## Reading the video file frame by frame
 video_frame_i = 0
 while True: #and video_frame_i < 850:
  
  ret, frame = video.read()
  if ret:  # time for a loop 7.28790903091 start from here
   h = frame.shape[0]
   w = frame.shape[1]
   video_frame_i += 1
   #print '\n\n########################################### start, frame', video_frame_i
  # print 'frame shape h,w:', h, w  # 1536 2304
  else:
   break
  
  if video_frame_i % 2 == 0:
  
   # time for a loop 7.43529987335,7.09782910347 variously, start from here
   use_focus_window = 1
   if use_focus_window:
    crop_x1 = 550  # 550
    crop_y1 = 700  #700# 300
    area_step_size = 740  #740# 640
    crop_x2 = crop_x1 + area_step_size * 1
    crop_y2 = crop_y1 + area_step_size * 1 * settings.h_resize / settings.w_resize
    frame_crop_color = frame[crop_y1:crop_y2, crop_x1:crop_x2]
    #cv2.imwrite('../testbench/frame_color_tmp.jpg', np.uint8(frame_crop_color))
   else:
    crop_x1 = 0
    crop_y1 = 0
    
    crop_x2 = 0 + w  # 2300  #1920
    crop_y2 = 0 + h  # 1536  #1080
    frame_crop_color = frame[crop_y1:crop_y2, crop_x1:crop_x2]

   # debug
   # print "shape:y1,y2,x1,x2:", crop_y1,", ", crop_y2,", ", crop_x1,", ", crop_x2
   # cv2.imshow("frame_cropped", frame_crop)
   # time for a loop 7.08207583427 from here
   
   #frame_crop = imutils.resize(cv2.cvtColor(frame_crop, cv2.COLOR_BGR2GRAY), width=320)
   # debug
   # print 'crop size', frame_crop.shape
   # cv2.imshow("TensorFlow Window", imutils.resize(frame_crop.astype(np.uint8), 480))

   frame_crop_roi, screen_out = do_segment(model, frame_crop_color, video_frame_i, 320, 320)

   # time elapsed for classification 0.0430190563202
   ######################### get classified result start#################################
   do_classification(frame,video_frame_i, frame_crop_roi, confMat2_TEST_Video, crop_y1, crop_y2, crop_x1,
                  crop_x2, n_classes,screen_out)
   
   ######################### Tensorflow
   '''
   # frame_demo = imutils.resize(frame, width = min(1200, settings.w_resize * 30)) #hy: choose a smaller window size
   cv2.imshow("Demo", frame_demo)
   cv2.waitKey(30)
   '''
   ######################################
   # cv2.waitKey(6000)
   if cv2.waitKey(1) & 0xFF == ord('q'):  # hy:press key-q to quit
    break
Ejemplo n.º 34
0
#########################################################################
####THIS CODE IS INTENDED TO REPRODUCE THE RESULTS OF EEGNET###
####Training with 12 data sets, validation with 4 datasets, and test with 10 datasets######
########################################################################
import os
import csv
import random
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc

from keras import backend as K
from keras.models import load_model
from keras.utils import np_utils

K.set_image_dim_ordering('th')  # set dimension ordering to theano

# import models
from EEGModels import EEGNet, EEGNetOrg
from EEGModels import DeepConvNet

# individuals number
train_sample_num = [
    2, 6, 7, 11, 12, 13, 14, 16, 17, 18, 20, 21, 22, 23, 24, 26
]
test_sample_num = [1, 3, 4, 5, 8, 9, 10, 15, 19, 25]
train_subject_num = len(train_sample_num)
test_subject_num = len(test_sample_num)
train_tot_trials = int(8 * 340)
test_tot_trials = int(test_subject_num * 340)
validation_tot_trials = int(4 * 340)
Ejemplo n.º 35
0
def seg_image_2c(h, w, file_i=1):
 print 'start evaluation, model', bg_model
 images, mask = tools.import_data_unet_2c("../tmp/distor_in/input/", "cad_%03d.jpg", "cad_m_%03d.jpg", h, w, 3, file_i,
                                          do_Flipping=False)
 # params: loadData(data_path, file_img, file_mask, h, w, maxNum, do_Flipping=False)
 test_num = (images.shape)[0]
 print 'number of test images', test_num, ', shape', images.shape  # (3, 1, 320, 320)
 
 images = images[1:-1, :, :, :]  #
 print '2-shape of test images', images.shape  # (1, 1, 320, 320)
 
 set_image_dim_ordering(dim_ordering='th')  # if not configured in
 try:
  model = load_model("../testbench/bg/" + bg_model)
  print 'loaded model', bg_model
 except Exception as e:
  print '[Hint-e]', e
 
 # debug
 # model.summary()
 
 mean, stdev = tools.calc_mean_stdev(images)
 
 images_original = images.copy()
 # print '3-number of test images', images_original.shape
 images = (images - mean) / stdev
 
 # print 'number of test images', (images.shape), ', ... ', images.shape
 
 for i in range(0, images.shape[0]):
  start = time.time()
  result = model.predict(images[i, :, :, :].reshape(1, 1, h, w), batch_size=1)  # old
  end = time.time()
  print(end - start)
  print 'Test image', i + 1, ", Min/Max: %f %f" % (np.min(result), np.max(result))
  print 'result shape', (result.shape)
  res = result[0, 0, :, :].reshape((h, w)) * 255  # old
  
  input_resized = cv2.resize(np.uint8(images_original[i, :, :, :].reshape(h, w) * 255), (480, 360))
  print 'min/max:%f %f' % (np.min(images_original[i, :, :, :]), np.max(images_original[i, :, :, :]))
  
  # debug
  # cv2.imshow("input_resized_window1", input_resized)
  # cv2.waitKey(10000)
  
  output_resized = cv2.resize(np.uint8(res), (480, 360))
  
  input_rgb = cv2.cvtColor(input_resized, cv2.COLOR_GRAY2RGB)
  
  # cv2.imshow("input_rgb_window2", input_rgb)
  # cv2.waitKey(10000)
  
  color = tools.add_colorOverlay(input_resized, output_resized)
  combined = np.hstack((input_rgb, color))
  save_file = True
  if save_file:
   cv2.imwrite('../testbench/res.jpg', np.uint8(res))
   # cv2.imwrite('../testbench/res_' + bg_model[:-5] + '%03d.jpg' % i, np.uint8(res))
   cv2.imwrite("../testbench/combined_%03d.jpg" % i, combined)
   cv2.imwrite("../testbench/color_%03d.jpg" % i, color)
  else:
   cv2.imshow('res', np.uint8(res))
  # cv2.imwrite('combined', combined)
  # cv2.imwrite('color', color)
  ##########################################################
  # res2 = result[0,0, :, 1].reshape((320,320))*255
  # back = result[0,0, :, 2].reshape((320, 320)) * 255
  # cv2.imshow("S", np.uint8(res))
  # cv2.imshow("S2", np.uint8(res2))
  # cv2.imshow("Bg", np.uint8(back))
  input = np.uint8(((images[i, :, :, :] * stdev + mean) * 255).reshape(320, 320))
  cv2.imshow("input", input)
  
  cv2.waitKey(10)
  return np.uint8(res)
Ejemplo n.º 36
0
# network and training
NB_EPOCH = 2
BATCH_SIZE = 128
VERBOSE = 1
OPTIMIZER = Adam()
VALIDATION_SPLIT = 0.2
IMG_ROWS, IMG_COLS = 28, 28  # input image dimensions
INPUT_SHAPE = (1, IMG_ROWS, IMG_COLS)  # data: shuffled and split between train and test sets

'''loading of the data and defining the no of classes'''
NB_CLASSES = 10  # number of outputs = number of digits
(X_train, y_train), (X_test, y_test) = mnist.load_data()


K.set_image_dim_ordering("th")  # consider them as float and normalize
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
# we need a 60K x [1 x 28 x 28] shape as input to the CONVNET
X_train = X_train[:, np.newaxis, :, :]
X_test = X_test[:, np.newaxis, :, :]
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = np_utils.to_categorical(y_train, NB_CLASSES)
y_test = np_utils.to_categorical(y_test, NB_CLASSES)
# initialize the optimizer and model

Ejemplo n.º 37
0
def EVALUATE_VIDEO_seg_and_classify(seg_model_name, VIDEO_FILE, num_class, in_ch, step_show=False, save_imgs=False,
                                    stop=False):  # (v)

  seg_model = os.path.join(FLAGS.seg_model_search_p, seg_model_name)
  # print 'seg model loaded:', seg_model
  video = cv2.VideoCapture(VIDEO_FILE)  # hy: changed from cv2.VideoCapture()
  video.set(1, 2)  # hy: changed from 1,2000 which was for wheelchair test video,
  # hy: propID=1 means 0-based index of the frame to be decoded/captured next

  if not video.isOpened():
    print "cannot find or open video file:", VIDEO_FILE
    exit(-1)
  eva_count = 0
  video_frame_i = 0  #

  confMat1_TEST = np.zeros((num_class, num_class), dtype=np.float)  # hy collect detailed confusion matrix
  confMat2_TEST = np.zeros((2, 2), dtype=np.float)
  # load seg model
  set_image_dim_ordering(dim_ordering='th')  #

  model = load_model(seg_model)
  while True and not stop:  # and video_frame_i < 850:
    ret, frame = video.read()
    if cv2.waitKey(1) & 0xFF == ord('q'):  # hy:press key-q to quit
      print 'key interrupt,press q again to quit completely'
      break

    if ret:  # time for a loop 7.28790903091 start from here
      h, w = frame.shape[0], frame.shape[1]
      video_frame_i += 1
      # print 'frame',video_frame_i
      # print 'frame shape h,w:', h, w  # 1536 2304

      if video_frame_i % 5 == 0:  # ' and video_frame_i > 3750:#> 1350 and video_frame_i < 1470:
        eva_count += 1
        # time for a loop 7.43529987335,7.09782910347 variously, start from here

        crop_x1 = 0
        crop_y1 = 0
        crop_x2 = 0 + w  # 2300  #1920
        crop_y2 = 0 + h  # 1536  #1080

        frame_crop = frame[crop_y1:crop_y2, crop_x1:crop_x2]
        frame_crop_color = frame_crop.copy()
        # debug
        ################################################################################################################

        pred_int, pred_255, fr_add_cont, old_mask, old_roi_res, new_mask, \
        pre_tensor, r1, r2, c1, c2, screen_out, roi_whitebg \
          = do_segment_video(model, frame_crop_color, video_frame_i, FLAGS.IMAGE_SIZE, FLAGS.IMAGE_SIZE,
                             in_ch, show_bbox=True)

      # add blue overlay for better demo

      tmp_h, tmp_w, tmp_ch = frame_crop_color.shape
      base = cv2.resize(frame_crop_color, (tmp_h, tmp_w))
      new_mask_display = cv2.resize(new_mask, (tmp_h, tmp_w))
      fr_add_cont_overlay = add_blueOverlay(base, new_mask_display)

      if screen_out:
        # prefix = FLAGS.save_res_path + get_model_index(Seg_MODEL_to_load, search_by='-')
        prefix = FLAGS.save_res_path
        fn = eva_count
        # VIDEO

        s_size = (800,
                  600)  # cv2.resize(pred_int, s_size) cv2.resize(old_mask, s_size) cv2.resize(roi_whitebg, s_size) new_mask frame_crop_color

        list_of_imgs = [cv2.resize(fr_add_cont_overlay, s_size)]
        winname = 'fr_with_newmask'
        stacked_imgs = create_stacked_n_col_images(prefix, fn, list_of_imgs, winname, save_im=True)
        # cv2.imshow('stacked_imgs',stacked_imgs)

      ################################################################################################################
      if not step_show:
        pass
      else:
        k = cv2.waitKey(30) & 0xFF
        while True and not stop:
          if k == ord('n'):
            print 'add to fail_list'
            # res_fail_list.append(read_path_im + os.path.basename(files_im[i]))
            # res_fail_list.append(files[i])
            break

          elif k == ord('y'):
            print 'add to pass_list'
            save_ori_frame = True
            save_imgs = True
            if save_imgs:
              if save_ori_frame:
                # im_save = cv2.resize(frame_crop_color, (1920, 1080), interpolation=cv2.INTER_CUBIC)  # upsampling
                im_save = frame_crop_color  # upsampling
              # or save image_crop_roi
              else:
                im_save = cv2.resize(frame_crop_color, (h, w))
              v_str = os.path.splitext(VIDEO_FILE)
              cv2.imwrite(FLAGS.save_res_path + v_str + '_' + str(video_frame_i) + '.jpg',
                          im_save)
              print 'saved to', FLAGS.save_res_path + v_str + '_' + str(video_frame_i) + '.jpg'
            break

          elif k == ord('q'):  # ESC
            break
          else:
            k = cv2.waitKey(30) & 0xFF
            if k != 255:
              print 'k:', k  # 81-l, 83-r, 82-u, 84-d

        if cv2.waitKey(1) & 0xFF == ord('q'):
          print 'key interrupt, press again to close all windows'
          break

      cv2.waitKey(10)  # required for roi_seg

  else:
    print 'video end'
    video.release()
    stop = True

  stop = True
  
  return stop
Ejemplo n.º 38
0
def process_tst(nfolds):
    FOLD_TO_CALC = [1, 2, 3, 4, 5]

    recalc = 1
    batch_size = 8
    augmentation_size = AUGMENTATION_SIZE
    cnn_type = 'ZF_full_keras_model_inception_v1'
    subm = pd.read_csv(INPUT_PATH + 'submission_format.csv',
                       index_col='filename')
    files = list(subm.index.values)
    print('Files to process: {}'.format(len(files)))

    pred_list = []
    for num_fold in range(1, nfolds + 1):
        print('Start KFold number {} from {}'.format(num_fold, nfolds))

        if num_fold not in FOLD_TO_CALC:
            continue

        cache_path = CACHE_PATH + cnn_type + '_fold_{}.pklz'.format(num_fold)
        if not os.path.isfile(cache_path) or recalc == 1:
            import keras.backend as K
            K.set_image_dim_ordering('th')
            model = ZF_full_keras_model_inception_v1()
            final_model_path = MODELS_PATH + '{}_fold_{}.h5'.format(
                cnn_type, num_fold)
            model.load_weights(final_model_path)

            steps = math.ceil(len(files) / batch_size)
            print('Steps: {}'.format(steps))
            full_preds = model.predict_generator(generator=predict_generator(
                files, batch_size),
                                                 steps=steps,
                                                 max_queue_size=10,
                                                 verbose=2)

            if full_preds.shape[0] != augmentation_size * len(files):
                print('Check predictions shape: {} != {}'.format(
                    full_preds.shape, augmentation_size * len(files)))
            print('Predictions shape: {}'.format(full_preds.shape))

            preds = []
            for j in range(len(files)):
                p = full_preds[j * augmentation_size:(j + 1) *
                               augmentation_size].copy().mean(axis=0)
                preds.append(p.copy())
            preds = np.array(preds)

            save_in_file(preds, cache_path)
        else:
            preds = load_from_file(cache_path)

        pred_list.append(preds)
    pred_list = np.array(pred_list)
    print('Full predictions shape: {}'.format(pred_list.shape))
    preds = pred_list.mean(axis=0)
    print('Averaged predictions shape: {}'.format(preds.shape))

    for i in range(preds.shape[0]):
        subm.loc[files[i], ANIMAL_TYPE] = preds[i]

    subm.to_csv(FEATURES_PATH + 'ZF_full_keras_model_inception_v1_test.csv')
Ejemplo n.º 39
0
def Xception(include_top=True, weights='imagenet',
             input_tensor=None):
    '''Instantiate the Xception architecture,
    optionally loading weights pre-trained
    on ImageNet. This model is available for TensorFlow only,
    and can only be used with inputs following the TensorFlow
    dimension ordering `(width, height, channels)`.
    You should set `image_dim_ordering="tf"` in your Keras config
    located at ~/.keras/keras.json.

    Note that the default input image size for this model is 299x299.

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.

    # Returns
        A Keras model instance.
    '''
    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')
    if K.backend() != 'tensorflow':
        raise Exception('The Xception model is only available with '
                        'the TensorFlow backend.')
    if K.image_dim_ordering() != 'tf':
        warnings.warn('The Xception model is only available for the '
                      'input dimension ordering "tf" '
                      '(width, height, channels). '
                      'However your settings specify the default '
                      'dimension ordering "th" (channels, width, height). '
                      'You should set `image_dim_ordering="tf"` in your Keras '
                      'config located at ~/.keras/keras.json. '
                      'The model being returned right now will expect inputs '
                      'to follow the "tf" dimension ordering.')
        K.set_image_dim_ordering('tf')
        old_dim_ordering = 'th'
    else:
        old_dim_ordering = None

    # Determine proper input shape
    if include_top:
        input_shape = (299, 299, 3)
    else:
        input_shape = (None, None, 3)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    x = Conv2D(32, 3, 3, subsample=(2, 2), bias=False, name='block1_conv1')(img_input)
    x = BatchNormalization(name='block1_conv1_bn')(x)
    x = Activation('relu', name='block1_conv1_act')(x)
    x = Conv2D(64, 3, 3, bias=False, name='block1_conv2')(x)
    x = BatchNormalization(name='block1_conv2_bn')(x)
    x = Activation('relu', name='block1_conv2_act')(x)

    residual = Conv2D(128, 1, 1, subsample=(2, 2),
                      border_mode='same', bias=False)(x)
    residual = BatchNormalization()(residual)

    x = SeparableConv2D(128, 3, 3, border_mode='same', bias=False, name='block2_sepconv1')(x)
    x = BatchNormalization(name='block2_sepconv1_bn')(x)
    x = Activation('relu', name='block2_sepconv2_act')(x)
    x = SeparableConv2D(128, 3, 3, border_mode='same', bias=False, name='block2_sepconv2')(x)
    x = BatchNormalization(name='block2_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), border_mode='same', name='block2_pool')(x)
    x = merge([x, residual], mode='sum')

    residual = Conv2D(256, 1, 1, subsample=(2, 2),
                      border_mode='same', bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block3_sepconv1_act')(x)
    x = SeparableConv2D(256, 3, 3, border_mode='same', bias=False, name='block3_sepconv1')(x)
    x = BatchNormalization(name='block3_sepconv1_bn')(x)
    x = Activation('relu', name='block3_sepconv2_act')(x)
    x = SeparableConv2D(256, 3, 3, border_mode='same', bias=False, name='block3_sepconv2')(x)
    x = BatchNormalization(name='block3_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), border_mode='same', name='block3_pool')(x)
    x = merge([x, residual], mode='sum')

    residual = Conv2D(728, 1, 1, subsample=(2, 2),
                      border_mode='same', bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block4_sepconv1_act')(x)
    x = SeparableConv2D(728, 3, 3, border_mode='same', bias=False, name='block4_sepconv1')(x)
    x = BatchNormalization(name='block4_sepconv1_bn')(x)
    x = Activation('relu', name='block4_sepconv2_act')(x)
    x = SeparableConv2D(728, 3, 3, border_mode='same', bias=False, name='block4_sepconv2')(x)
    x = BatchNormalization(name='block4_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), border_mode='same', name='block4_pool')(x)
    x = merge([x, residual], mode='sum')

    for i in range(8):
        residual = x
        prefix = 'block' + str(i + 5)

        x = Activation('relu', name=prefix + '_sepconv1_act')(x)
        x = SeparableConv2D(728, 3, 3, border_mode='same', bias=False, name=prefix + '_sepconv1')(x)
        x = BatchNormalization(name=prefix + '_sepconv1_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv2_act')(x)
        x = SeparableConv2D(728, 3, 3, border_mode='same', bias=False, name=prefix + '_sepconv2')(x)
        x = BatchNormalization(name=prefix + '_sepconv2_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv3_act')(x)
        x = SeparableConv2D(728, 3, 3, border_mode='same', bias=False, name=prefix + '_sepconv3')(x)
        x = BatchNormalization(name=prefix + '_sepconv3_bn')(x)

        x = merge([x, residual], mode='sum')

    residual = Conv2D(1024, 1, 1, subsample=(2, 2),
                      border_mode='same', bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block13_sepconv1_act')(x)
    x = SeparableConv2D(728, 3, 3, border_mode='same', bias=False, name='block13_sepconv1')(x)
    x = BatchNormalization(name='block13_sepconv1_bn')(x)
    x = Activation('relu', name='block13_sepconv2_act')(x)
    x = SeparableConv2D(1024, 3, 3, border_mode='same', bias=False, name='block13_sepconv2')(x)
    x = BatchNormalization(name='block13_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), border_mode='same', name='block13_pool')(x)
    x = merge([x, residual], mode='sum')

    x = SeparableConv2D(1536, 3, 3, border_mode='same', bias=False, name='block14_sepconv1')(x)
    x = BatchNormalization(name='block14_sepconv1_bn')(x)
    x = Activation('relu', name='block14_sepconv1_act')(x)

    x = SeparableConv2D(2048, 3, 3, border_mode='same', bias=False, name='block14_sepconv2')(x)
    x = BatchNormalization(name='block14_sepconv2_bn')(x)
    x = Activation('relu', name='block14_sepconv2_act')(x)

    if include_top:
        x = GlobalAveragePooling2D(name='avg_pool')(x)
        x = Dense(1000, activation='softmax', name='predictions')(x)

    # Create model
    model = Model(img_input, x)

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file('xception_weights_tf_dim_ordering_tf_kernels.h5',
                                    TF_WEIGHTS_PATH,
                                    cache_subdir='models')
        else:
            weights_path = get_file('xception_weights_tf_dim_ordering_tf_kernels_notop.h5',
                                    TF_WEIGHTS_PATH_NO_TOP,
                                    cache_subdir='models')
        model.load_weights(weights_path)

    if old_dim_ordering:
        K.set_image_dim_ordering(old_dim_ordering)
    return model
Ejemplo n.º 40
0
def main():
    testsetCount = 5
    accuracies = [0, 0, 0,0,0]

    for testid in range(0,testsetCount):
        print(testid)

        K.set_image_dim_ordering('tf')
        #sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

        # from keras_video_classifier.library.utility.plot_utils import plot_and_save_history
        # from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
        # from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf
        #
        data_set_name = 'AM_pics_' + str(testid)
        # input_dir_path = os.path.join(os.path.dirname(__file__), 'AM_data')
        # output_dir_path = os.path.join(os.path.dirname(__file__), 'models', data_set_name)
        # report_dir_path = os.path.join(os.path.dirname(__file__), 'reports', data_set_name)
        #
        # np.random.seed(34)
        #
        # # this line downloads the video files of UCF-101 dataset if they are not available in the very_large_data folder
        # #load_ucf(input_dir_path)
        #
        # classifier = VGG16LSTMVideoClassifier()
        #
        # history = classifier.fit(data_dir_path=input_dir_path, model_dir_path=output_dir_path, vgg16_include_top=False,
        #                          data_set_name=data_set_name,from_picture=True)
        #
        # plot_and_save_history(history, VGG16LSTMVideoClassifier.model_name,
        #                       report_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-hi-dim-history_' + str(testid) + '.png')

        print("predicting now !!!!!")
        K.set_image_dim_ordering('tf')
        sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

        from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
        from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf, scan_ucf_with_labels

        vgg16_include_top = False
        data_dir_path = os.path.join(os.path.dirname(__file__), 'AM_data')
        model_dir_path = os.path.join(os.path.dirname(__file__), 'models', data_set_name)
        # data_dir_path = os.path.join(os.path.dirname(__file__), 'very_large_data')
        # model_dir_path = os.path.join(os.path.dirname(__file__), 'models', 'UCF-101')
        config_file_path = VGG16LSTMVideoClassifier.get_config_file_path(model_dir_path,
                                                                         vgg16_include_top=vgg16_include_top)
        weight_file_path = VGG16LSTMVideoClassifier.get_weight_file_path(model_dir_path,
                                                                         vgg16_include_top=vgg16_include_top)

        print("Reading weights from :", weight_file_path)
        print("Reading Config from :", config_file_path)

        np.random.seed(33)
        print(weight_file_path)
        # load_ucf(data_dir_path)

        predictor = VGG16LSTMVideoClassifier()
        predictor.load_model(config_file_path, weight_file_path)

        videos = scan_ucf_with_labels(data_dir_path, [label for (label, label_index) in predictor.labels.items()],testid)

        video_file_path_list = np.array([file_path for file_path in videos.keys()])
        np.random.shuffle(video_file_path_list)

        correct_count = 0
        count = 0

        for video_file_path in video_file_path_list:
            label = videos[video_file_path]
            predicted_label = predictor.predict(video_file_path, from_picture=True)
            print('predicted: ' + predicted_label + ' actual: ' + label)
            correct_count = correct_count + 1 if label == predicted_label else correct_count
            count += 1
            accuracy = correct_count / count
            print('accuracy: ', accuracy)
            accuracies[testid] = accuracy

    print(accuracies)
    print(sum(accuracies) / len(accuracies))
Ejemplo n.º 41
0
def Xception(include_top=True, weights='imagenet', input_tensor=None):
    '''Instantiate the Xception architecture,
    optionally loading weights pre-trained
    on ImageNet. This model is available for TensorFlow only,
    and can only be used with inputs following the TensorFlow
    dimension ordering `(width, height, channels)`.
    You should set `image_dim_ordering="tf"` in your Keras config
    located at ~/.keras/keras.json.

    Note that the default input image size for this model is 299x299.

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.

    # Returns
        A Keras model instance.
    '''
    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')
    if K.backend() != 'tensorflow':
        raise Exception('The Xception model is only available with '
                        'the TensorFlow backend.')
    if K.image_dim_ordering() != 'tf':
        warnings.warn('The Xception model is only available for the '
                      'input dimension ordering "tf" '
                      '(width, height, channels). '
                      'However your settings specify the default '
                      'dimension ordering "th" (channels, width, height). '
                      'You should set `image_dim_ordering="tf"` in your Keras '
                      'config located at ~/.keras/keras.json. '
                      'The model being returned right now will expect inputs '
                      'to follow the "tf" dimension ordering.')
        K.set_image_dim_ordering('tf')
        old_dim_ordering = 'th'
    else:
        old_dim_ordering = None

    # Determine proper input shape
    if include_top:
        input_shape = (32, 32, 1)
    else:
        input_shape = (None, None, 3)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    x = Conv2D(32, 3, 3, bias=False, name='new_block1_conv1')(img_input)
    x = BatchNormalization(name='block1_conv1_bn')(x)
    x = Activation('relu', name='block1_conv1_act')(x)
    x = Conv2D(64, 3, 3, bias=False, name='block1_conv2')(x)
    x = BatchNormalization(name='block1_conv2_bn')(x)
    x = Activation('relu', name='block1_conv2_act')(x)

    residual = Conv2D(128,
                      1,
                      1,
                      subsample=(2, 2),
                      border_mode='same',
                      bias=False)(x)
    residual = BatchNormalization()(residual)

    x = SeparableConv2D(128,
                        3,
                        3,
                        border_mode='same',
                        bias=False,
                        name='block2_sepconv1')(x)
    x = BatchNormalization(name='block2_sepconv1_bn')(x)
    x = Activation('relu', name='block2_sepconv2_act')(x)
    x = SeparableConv2D(128,
                        3,
                        3,
                        border_mode='same',
                        bias=False,
                        name='block2_sepconv2')(x)
    x = BatchNormalization(name='block2_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     border_mode='same',
                     name='block2_pool')(x)
    x = merge([x, residual], mode='sum')

    residual = Conv2D(256,
                      1,
                      1,
                      subsample=(2, 2),
                      border_mode='same',
                      bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block3_sepconv1_act')(x)
    x = SeparableConv2D(256,
                        3,
                        3,
                        border_mode='same',
                        bias=False,
                        name='block3_sepconv1')(x)
    x = BatchNormalization(name='block3_sepconv1_bn')(x)
    x = Activation('relu', name='block3_sepconv2_act')(x)
    x = SeparableConv2D(256,
                        3,
                        3,
                        border_mode='same',
                        bias=False,
                        name='block3_sepconv2')(x)
    x = BatchNormalization(name='block3_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     border_mode='same',
                     name='block3_pool')(x)
    x = merge([x, residual], mode='sum')

    residual = Conv2D(728,
                      1,
                      1,
                      subsample=(2, 2),
                      border_mode='same',
                      bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block4_sepconv1_act')(x)
    x = SeparableConv2D(728,
                        3,
                        3,
                        border_mode='same',
                        bias=False,
                        name='block4_sepconv1')(x)
    x = BatchNormalization(name='block4_sepconv1_bn')(x)
    x = Activation('relu', name='block4_sepconv2_act')(x)
    x = SeparableConv2D(728,
                        3,
                        3,
                        border_mode='same',
                        bias=False,
                        name='block4_sepconv2')(x)
    x = BatchNormalization(name='block4_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     border_mode='same',
                     name='block4_pool')(x)
    x = merge([x, residual], mode='sum')

    for i in range(8):
        residual = x
        prefix = 'block' + str(i + 5)

        x = Activation('relu', name=prefix + '_sepconv1_act')(x)
        x = SeparableConv2D(728,
                            3,
                            3,
                            border_mode='same',
                            bias=False,
                            name=prefix + '_sepconv1')(x)
        x = BatchNormalization(name=prefix + '_sepconv1_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv2_act')(x)
        x = SeparableConv2D(728,
                            3,
                            3,
                            border_mode='same',
                            bias=False,
                            name=prefix + '_sepconv2')(x)
        x = BatchNormalization(name=prefix + '_sepconv2_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv3_act')(x)
        x = SeparableConv2D(728,
                            3,
                            3,
                            border_mode='same',
                            bias=False,
                            name=prefix + '_sepconv3')(x)
        x = BatchNormalization(name=prefix + '_sepconv3_bn')(x)

        x = merge([x, residual], mode='sum')

    residual = Conv2D(1024,
                      1,
                      1,
                      subsample=(2, 2),
                      border_mode='same',
                      bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block13_sepconv1_act')(x)
    x = SeparableConv2D(728,
                        3,
                        3,
                        border_mode='same',
                        bias=False,
                        name='block13_sepconv1')(x)
    x = BatchNormalization(name='block13_sepconv1_bn')(x)
    x = Activation('relu', name='block13_sepconv2_act')(x)
    x = SeparableConv2D(1024,
                        3,
                        3,
                        border_mode='same',
                        bias=False,
                        name='block13_sepconv2')(x)
    x = BatchNormalization(name='block13_sepconv2_bn')(x)

    # x = MaxPooling2D((3, 3), strides=(2, 2), border_mode='same', name='block13_pool')(x)
    # x = merge([x, residual], mode='sum')

    # x = SeparableConv2D(1536, 3, 3, border_mode='same', bias=False, name='block14_sepconv1')(x)
    # x = BatchNormalization(name='block14_sepconv1_bn')(x)
    # x = Activation('relu', name='block14_sepconv1_act')(x)

    # x = SeparableConv2D(2048, 3, 3, border_mode='same', bias=False, name='block14_sepconv2')(x)
    # x = BatchNormalization(name='block14_sepconv2_bn')(x)
    # x = Activation('relu', name='block14_sepconv2_act')(x)

    if include_top:
        x = GlobalAveragePooling2D(name='avg_pool')(x)
        x = Dense(2, activation='softmax', name='new_predictions')(x)

    # Create model
    model = Model(img_input, x)

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file(
                'xception_weights_tf_dim_ordering_tf_kernels.h5',
                TF_WEIGHTS_PATH,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'xception_weights_tf_dim_ordering_tf_kernels_notop.h5',
                TF_WEIGHTS_PATH_NO_TOP,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)

    if old_dim_ordering:
        K.set_image_dim_ordering(old_dim_ordering)
    return model
 def start_bundle(self, context=None):
     os.environ["KERAS_BACKEND"] = "theano"
     import vgg16bn
     from keras import backend as K
     K.set_image_dim_ordering('th')
     self.vgg = vgg16bn.Vgg16BN(include_top=False, size=self.size)
Ejemplo n.º 43
0
# https://www.kaggle.com/danielelton/digit-recognizer/keras-cnn-with-explanation

import pandas as pd
import numpy as np
from keras.utils.np_utils import to_categorical
from keras import backend as K
from keras.models import Sequential
from keras.layers.convolutional import *
from keras.layers.core import Dropout, Dense, Flatten, Activation
from keras.callbacks import EarlyStopping
import os

K.set_image_dim_ordering('th') #input shape: (channels, height, width)

root_dir = os.path.abspath('../..')
data_dir = os.path.join(root_dir, 'data_examples', 'DigitRecognizer')


train_df = pd.read_csv(os.path.join(data_dir, 'train.csv'))
valid_df = pd.read_csv(os.path.join(data_dir, 'test.csv'))

x_train = train_df.drop(['label'], axis=1).values.astype('float32')
Y_train = train_df['label'].values.astype('float32')
x_valid = valid_df.values.astype('float32')

img_width, img_height = 28, 28

n_train = x_train.shape[0]
n_valid = x_valid.shape[0]

n_classes = 10
Ejemplo n.º 44
0
from keras.layers import Dense, LSTM, Dropout, Conv1D, Conv2D, MaxPooling2D, Flatten
from keras.callbacks import ModelCheckpoint
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
import pandas as pd
import scipy.io as sio
from os import listdir
from os.path import isfile, join
import numpy as np
import keras
from sklearn.metrics import accuracy_score
from keras import backend as K
import sys
from ultis import load_data

K.set_image_dim_ordering('tf')  # For problems with ordering

number_of_classes = 4


def change(x):
    answer = np.zeros((np.shape(x)[0]))
    for i in range(np.shape(x)[0]):
        max_value = max(x[i, :])
        max_index = list(x[i, :]).index(max_value)
        answer[i] = max_index
    return answer.astype(np.int)


file_list = listdir('./cnn_data/')
file_list = [file for file in file_list if file.endswith('.npy')]
Ejemplo n.º 45
0
def train_2c(PROJ_DIR, train_mode, model_path, model_con_name, data_path, test_data_path, log_LABEL,
             learning_rate, batch_size, MAX_ITERATION, INPUT_SIZE,
             rnd_blurriness_min, rnd_blurriness_max, rnd_darkness_min, rnd_darkness_max, dropouts):
  print ('PROJ_DIR,train_mode,model_path,model_con_name,data_path,test_data_path,log_LABEL,\
    learning_rate,batch_size,MAX_ITERATION,INPUT_SIZE,rnd_blurriness_min,rnd_blurriness_max,\
    rnd_darkness_min,rnd_darkness_max,dropouts',
         PROJ_DIR, train_mode, model_path, model_con_name, data_path, test_data_path, log_LABEL,
         learning_rate, batch_size, MAX_ITERATION, INPUT_SIZE,
         rnd_blurriness_min, rnd_blurriness_max, rnd_darkness_min, rnd_darkness_max, dropouts)

  model_path_name = os.path.join(model_path, model_con_name)

  if 'con' in train_mode:
    # manual
    print '\nfollow model:', model_path_name, 'learning rate:', learning_rate
    set_image_dim_ordering(dim_ordering='th')

  h, w = INPUT_SIZE, INPUT_SIZE
  if 'con' not in train_mode:
    model_path_name = model_path + '.hdf5'

  print '\ntrain binary classes, load data,learning rate:', learning_rate

  images, masks = load_and_preprocess_data_k(h, w, PROJ_DIR, data_path,
                                             rnd_blurriness_min, rnd_blurriness_max, rnd_darkness_min, rnd_darkness_max)
  print 'train len:', len(images)

  images_t, masks_t = load_and_preprocess_data_onlinetest_k(h, w, PROJ_DIR, test_data_path,
                                                            rnd_blurriness_min, rnd_blurriness_max, rnd_darkness_min,
                                                            rnd_darkness_max)

  print 'images shape', images.shape

  # images = images.transpose((None, 1, h, w))
  print 'set checkpoint'

  save_params = ModelCheckpoint(filepath=model_path + log_LABEL + '_{epoch:02d}-{val_loss:.2f}.hdf5',
                                monitor='val_loss', verbose=2,
                                save_best_only=False, save_weights_only=False, mode='auto')

  keras.callbacks.History()
  epochs = MAX_ITERATION  # 
  learning_rate = learning_rate  # 
  # decay_rate = learning_rate / epochs #not so good
  momentum = 0.99
  sgd = arch.SGD(lr=learning_rate, momentum=momentum)  #
  set_image_dim_ordering(dim_ordering='th')

  print 'load network'
  if train_mode == 'new_train':
    model = arch.segnet_arch_2c(dropouts, h, w)

  if train_mode == 'con_train':
    model = load_model(model_path_name)

  print 'compile'
  # compile option 1
  model.compile(loss='binary_crossentropy', optimizer=sgd)

  model.fit(images, masks, batch_size=batch_size, nb_epoch=epochs, verbose=1, shuffle=True,
            validation_data=(images_t, masks_t), callbacks=[save_params])

  # visulization
  # verbose=1 to switch on printing batch result

  print 'save'
  model.save(model_path + 'model_' + log_LABEL + '.h5')
Ejemplo n.º 46
0
import numpy as np
from tqdm import tqdm
from keras.models import Model
from keras.layers import Dense, Dropout, Flatten, Input, Conv2D, Reshape
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D
from keras.optimizers import Adam
from keras import backend
from keras import initializers
# set keras/tensorflow configuration as channel-first
backend.set_image_dim_ordering('th')

# set parameters and load dataset
randomDim = 100
dLosses = []
gLosses = []
adam = Adam(lr=0.0002, beta_1=0.5)
X_train = np.load('X_13styles.npy')
# set the dataset as channel-first and reshape it between -1 and 1
X_train = X_train.transpose((0,3,1,2))
X_train = X_train.astype(np.float32)/127.5 - 1.0

# generator
g_input = Input(shape=(randomDim,))
g = Dense(256*8*8,
          kernel_initializer=initializers.RandomNormal(stddev=0.02))(g_input)
g = LeakyReLU(0.2)(g)
g = Reshape((256, 8, 8))(g)
g = UpSampling2D(size=(2, 2))(g)
g = Conv2D(128, kernel_size=(5, 5), padding='same')(g)
g = LeakyReLU(0.2)(g)
def train(args):

    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = args.dev

    #todo: manage parameters in main
    if args.data == "big":
        dataset_path = "/cvgl/group/GazeCapture/gazecapture"
    if args.data == "small":
        dataset_path = "eye_tracker_train_and_val.npz"

    if args.data == "big":
        train_path = "/cvgl/group/GazeCapture/train"
        val_path = "/cvgl/group/GazeCapture/validation"
        test_path = "/cvgl/group/GazeCapture/test"

    print("{} dataset: {}".format(args.data, dataset_path))
    print('train111111111111')
    # train parameters
    n_epoch = args.max_epoch
    batch_size = args.batch_size
    patience = args.patience
    print('train22222222222222')
    # image parameter
    img_cols = 64
    img_rows = 64
    img_ch = 3

    # model
    K.set_image_dim_ordering('th')
    model = get_eye_tracker_model(img_ch, img_cols, img_rows)
    print(img_ch)

    # model summary
    model.summary()
    print('train33333333333333333333')
    # weights
    # print("Loading weights...",  end='')
    # weights_path = "weights/weights.003-4.05525.hdf5"
    # model.load_weights(weights_path)
    # print("Done.")

    # optimizer
    sgd = SGD(lr=1e-1, decay=5e-4, momentum=9e-1, nesterov=True)
    adam = Adam(lr=1e-3)

    # compile model
    model.compile(optimizer=adam, loss='mse')

    # data
    # todo: parameters not hardocoded
    if args.data == "big":
        # train data
        train_names = load_data_names(train_path)
        # validation data
        val_names = load_data_names(val_path)
        # test data
        test_names = load_data_names(test_path)
    print('train444444444444444444444')
    if args.data == "small":
        train_data, val_data = load_data_from_npz(dataset_path)

    # debug
    # x, y = load_batch([l[0:batch_size] for l in train_data], img_ch, img_cols, img_rows)
    # x, y = load_batch_from_names(train_names[0:batch_size], dataset_path, img_ch, img_cols, img_rows)

    # last dataset checks
    if args.data == "small":
        print("train data sources of size: {} {} {} {} {}".format(
            train_data[0].shape[0], train_data[1].shape[0], train_data[2].shape[0],
            train_data[3].shape[0], train_data[4].shape[0]))
        print("validation data sources of size: {} {} {} {} {}".format(
            val_data[0].shape[0], val_data[1].shape[0], val_data[2].shape[0],
            val_data[3].shape[0], val_data[4].shape[0]))


    if args.data == "big":
        model.fit_generator(
            generator=generator_train_data(train_names, dataset_path, batch_size, img_ch, img_cols, img_rows),
            steps_per_epoch=(len(train_names)) / batch_size,
            epochs=n_epoch,
            verbose=1,
            validation_data=generator_val_data(val_names, dataset_path, batch_size, img_ch, img_cols, img_rows),
            validation_steps=(len(val_names)) / batch_size,
            callbacks=[EarlyStopping(patience=patience),
                       ModelCheckpoint("weights_big/weights.{epoch:03d}-{val_loss:.5f}.hdf5", save_best_only=True)
                       ]
        )
    if args.data == "small":
        model.fit_generator(
            generator=generator_npz(train_data, batch_size, img_ch, img_cols, img_rows),
            steps_per_epoch=(train_data[0].shape[0])/batch_size,
            epochs=n_epoch,
            verbose=1,
            validation_data=generator_npz(val_data, batch_size, img_ch, img_cols, img_rows),
            validation_steps=(val_data[0].shape[0])/batch_size,
            callbacks=[EarlyStopping(patience=patience),
                       ModelCheckpoint("weights/weights.{epoch:03d}-{val_loss:.5f}.hdf5", save_best_only=True)
                       ]
        )
Ejemplo n.º 48
0
def foo():

    # Determine proper input shape
    if keras.__version__ > '1.0.3':
        K.set_image_dim_ordering('th')
    input_shape = (1, 224, 224)

    #img_input = Input(shape=input_shape)

    model = Sequential()

    model.add(
        Convolution2D(32,
                      3,
                      3,
                      input_shape=input_shape,
                      init=weight_init,
                      name='conv1_1'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Convolution2D(32, 3, 3, init=weight_init, name='conv1_2'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(dropout))

    model.add(Convolution2D(64, 3, 3, init=weight_init, name='conv2_1'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Convolution2D(64, 3, 3, init=weight_init, name='conv2_2'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Convolution2D(64, 3, 3, init=weight_init, name='conv2_3'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(dropout))

    model.add(Convolution2D(128, 3, 3, init=weight_init, name='conv3_1'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Convolution2D(128, 3, 3, init=weight_init, name='conv3_2'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(dropout))

    model.add(
        Convolution2D(512,
                      3,
                      3,
                      init=weight_init,
                      border_mode='same',
                      name='conv4_1'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      init=weight_init,
                      border_mode='same',
                      name='conv4_2'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(dropout))

    model.add(Flatten())
    model.add(Dense(120, init=weight_init))
    model.add(Activation('relu'))
    model.add(Dropout(dropout))

    model.add(Dropout(dropout))
    model.add(Dense(2))
    model.add(Activation('sigmoid'))

    return model
Ejemplo n.º 49
0
    assert args.dset in ["mnist", "celebA"]

    # Set the backend by modifying the env variable
    if args.backend == "theano":
        os.environ["KERAS_BACKEND"] = "theano"
    elif args.backend == "tensorflow":
        os.environ["KERAS_BACKEND"] = "tensorflow"

    # Import the backend
    import keras.backend as K

    # manually set dim ordering otherwise it is not changed
    if args.backend == "theano":
        image_dim_ordering = "th"
        K.set_image_dim_ordering(image_dim_ordering)
    elif args.backend == "tensorflow":
        image_dim_ordering = "tf"
        K.set_image_dim_ordering(image_dim_ordering)

    import train_GAN

    # Set default params
    d_params = {"mode": "train_GAN",
                "dset": args.dset,
                "generator": args.generator,
                "batch_size": args.batch_size,
                "n_batch_per_epoch": args.n_batch_per_epoch,
                "nb_epoch": args.nb_epoch,
                "model_name": "CNN",
                "epoch": args.epoch,
Ejemplo n.º 50
0
    theano.config.nvcc.flags = '-D_FORCE_INLINES'
    theano.config.cxx = os.environ['CONDA_PREFIX'] + '/bin/g++'
else:
    raise Exception('No configuration found when the backend is ' +
                    os.environ['KERAS_BACKEND'])

# import and check Keras version
import keras
import keras.backend as K
from keras import __version__ as keras_version
from pkg_resources import parse_version
if (parse_version(keras_version) >= parse_version('2.0')):
    raise RuntimeError('DeepCell requires Keras 1 to run')

# configure Keras, to avoid using file ~/.keras/keras.json
K.set_image_dim_ordering('th')  # theano's image format (required by DeepCell)
K.set_floatx('float32')
K.set_epsilon('1e-07')

##
###############################################################################

from __future__ import print_function
from keras.optimizers import SGD, RMSprop
from keras.callbacks import ModelCheckpoint, LearningRateScheduler

import cytometer
from cytometer.deepcell import rate_scheduler, train_model_sample, get_data_sample
from cytometer.deepcell_models import bn_feature_net_61x61 as the_model

dataset = "3T3_all_61x61"
Ejemplo n.º 51
0
#sklearn
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score


# Import libraries
import os,cv2
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image

K.set_image_dim_ordering('tf')

#important Params 
img_rows, img_cols = 200, 200  # input image dimensions #460, 480
img_channels = 1 # number of channels (Gray Scale)
num_epoch=5      # Training epoch
batch_size = 100  # batch_size to train
nb_pool = 2       # size of pooling area for max pooling
stride = 2        # ?????? 
num_classes = 2	  # Define the number of classes
nb_conv = 3	  # convolution kernel size


# Define data path
# get the current directory 
PATH_1 = os.getcwd()
Ejemplo n.º 52
0
def build_keras_model(args, modelEpsilon, input_shape, hiddenShrinkage,  M , y , M_validation = None, y_validation = None) :
    hLayerCount = args.hidCount
    k.set_image_dim_ordering('tf') # otherwise it would give 'negative dimension size' for maxpool operations
    #k.image_dim_ordering()
    BNEnabled = int(args.bnorm) == 1

    decay_Enabled = int(args.lr_decay) == 1

    shrinkage = hiddenShrinkage

   # k.set_image_dim_ordering('tf')
    
    tf.set_random_seed(args.randomSeed) ## tf runs off a different random generator, let it be random for now, to be able to see if it really is the random init that is causing the 'zero' results?

    if args.optimizer == 1 : 
        optimizer=Adam(lr=args.learnRate,    epsilon=modelEpsilon, beta_1=args.momentum, beta_2=0.999, 	decay=args.lr_decay)  # for float16, otherwise we get NaNs
    else :
        optimizer = SGD(lr=args.learnRate, momentum=args.momentum, decay=args.lr_decay)


	 # , beta_1=0.99, since the new Batchnorm logic this isn't needed
    loss = 'mean_squared_error'
    accMetrc = 'mae'


    # Set up the base model.
    model = Sequential()
    
    lastLayerSize = args.firstLayerSize #lastLayerSize_MAX
    # Input = knet_main.knnLayer( myNet,np.array([-1]), knet_main.LAYER_SUBTYPE_INPUT)

    w_reg = l1_l2(l1=0.0, l2=hiddenShrinkage)
 #    if conv was enabled we then do NOT regularize stuff at the first FC layer as we only want to regularize by h2 once
    if args.convLayers > 0 :
        lastOutput = input_shape[0] # for conv nets it is channels last format for Tensorflow, so the first element of the input shape is the actual number of SNPs
        print("Adding "+str(args.convLayers)+" conv layers, with initial input dimension: " + str(lastOutput), flush=True)

        currentNumFilters= args.convFilters
        currentStride = 3
        filter_size = 5 # as it turns out it is not actually a problem if the conv outputs something that isn't an integer, so we just need to downsample it
        
        model.add(Conv1D(currentNumFilters, filter_size, input_shape=(input_shape),kernel_regularizer=w_reg, kernel_initializer='he_normal' , padding="same", strides=currentStride ))
        if BNEnabled : model.add(BatchNormalization())    
        addActivation(model,args.hidAct)
        if args.dropout != -1 : model.add(Dropout(args.dropout))
    
    
    # k.floatx() # 'float32'
  #  args.convLayers = 2


        lastOutput = (lastOutput - filter_size +2) / currentStride + 1
        lastOutput = int(lastOutput) # as these can only be integers
        print("filter size : " + str(filter_size), flush=True)
        #i=1
        shrinkage = 0.0
        currentStride = 1
        pool_size = 2
        for i in range(1, args.convLayers +1) :
            
            # decide on filter size, depending on input, Conv layers must always produce even outputs so that maxpool can half them
            filter_size = 3
            if lastOutput % 2 != 0 : filter_size = 4 # if the current output is not even, then we have to use a filter size of 4, otherwise we get fractions after the maxpool operation
            ## currentNumFilters = (i+1) * args.convFilters
            currentNumFilters = currentNumFilters // 2
            
            
            model.add(Conv1D(currentNumFilters, filter_size,kernel_regularizer=None, kernel_initializer='he_normal' , padding="same", strides=currentStride))
            if BNEnabled : model.add(BatchNormalization())    
            addActivation(model,args.hidAct)
            if args.dropout != -1 : model.add(Dropout(args.dropout))
            
            
            lastOutput = (lastOutput - filter_size +2) / currentStride + 1
            lastOutput = int(lastOutput) # as these can only be integers
            print("filter size affter Conv ("+str(i)+") : " + str(filter_size) + " / output: " + str(lastOutput), flush=True)
            
            lastOutput = (lastOutput - pool_size) / pool_size + 1 # compute what dimensions the conv+maxpool operations are going to leave for the next layer
            print("filter size affter Maxpool ("+str(i)+") : " + str(filter_size) + " / output: " + str(lastOutput), flush=True)
            model.add(MaxPooling1D())  # the default is 2
            
        model.add(Flatten()) # Flatten the data for input into the plain hidden layer
        


    for i in range(1,hLayerCount+1) : # iterate 1 based, otherwise we will get a reduction after the first layer, no matter the widthReductionRate, as 0 is divisible by anything
        if i > 1 or args.convLayers > 0 : shrinkageParam = w_reg # only add regularizer for first layer, subsequent layers will always have none  
        else : shrinkageParam = None
        #if i == (hLayerCount-1) : lastWidth = 2 # enforce so that the last widht is always 2, ie 1 neuron makes it MORE like the other LESS likely
    
 
        if i == 1:  model.add(Dense(lastLayerSize,kernel_regularizer=shrinkageParam, kernel_initializer='he_normal', input_shape = input_shape)) 
        else : model.add(Dense(lastLayerSize,kernel_regularizer=shrinkageParam, kernel_initializer='he_normal'))
        
        if BNEnabled : model.add(BatchNormalization())
        addActivation(model,args.hidAct)
        
        if args.dropout != -1 : model.add(Dropout(args.dropout))
        
        print("added layer at depth: " + str(i) + " with width: " + str(lastLayerSize) + " / shrinkage: " + str(shrinkage))
        
        # control the 'fatness' of the network: we reduce the width at a given rate: if this is 1, then at every subsequent layer, if its 2, then every 2nd layer etc
        if i % args.widthReductionRate == 0 :  lastLayerSize = lastLayerSize // 2
        
        if lastLayerSize < 2 : break # if 
        
    #
    if hLayerCount == 0 : model.add(Dense(1, kernel_initializer='he_normal',kernel_regularizer=w_reg, input_shape = input_shape))	
    else : model.add(Dense(1, kernel_initializer='he_normal',kernel_regularizer=shrinkageParam))	
    
    if len( y.shape) > 1 :  model.add(Activation('softmax'))
    
    
    # Compile the model.	
    model.compile(loss=loss, optimizer=optimizer, metrics=[accMetrc])
    
    return(model)
Ejemplo n.º 53
0
import os
from config import config
from mhd_util import *
from NetModel_conc import *
import matplotlib.pyplot as pl
working_path = "/home/customer/document/lung-nodule-detection/data/"
weights_path = "/home/customer/document/lung-nodule-detection/data/weights/"
output_path = "/home/customer/document/lung-nodule-detection/data/output/"

window=48
out_w=44
height = window
width = window
depth = window
batch_size=1
K.set_image_dim_ordering('th')  # Theano dimension ordering in this code
crop_size=2
data_form=np.float32
shape=0

ClassRoot='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/Data/'
if __name__ == '__main__':
    model=ClassNet_FlexWindow()
    #model36=ClassNet_36()
    #model24=ClassNet_24()
    #plot(model,to_file='/home/customer/document/lung-nodule-detection/huangyj/ClassModel.png',show_shapes=True)
    
    #plot_model(model,to_file='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/ClassModel48.png',show_shapes=True)
    #plot_model(model36,to_file='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/ClassModel36.png',show_shapes=True)
    #plot_model(model24,to_file='/home/customer/document/lung-nodule-detection/challenge/ClassTrain/ClassModel24.png',show_shapes=True)
    use_existing=True
def alexnet_model(weights_path=None):
    """
    Returns a keras model for AlexNet, achieving roughly 80% at ImageNet2012 validation set
    
    Model and weights from
    https://github.com/heuritech/convnets-keras/blob/master/convnetskeras/convnets.py
    and only slightly modified to work with TF backend
    """

    K.set_image_dim_ordering('th')
    inputs = Input(shape=(3, 227, 227))

    conv_1 = Conv2D(96, 11, strides=4, activation='relu', name='conv_1')(inputs)

    conv_2 = MaxPooling2D((3, 3), strides=(2, 2))(conv_1)
    conv_2 = cross_channel_normalization(name="convpool_1")(conv_2)
    conv_2 = ZeroPadding2D((2, 2))(conv_2)
    conv_2 = merge([
        Conv2D(128, 5, activation="relu", name='conv_2_' + str(i + 1))
        (split_tensor(ratio_split=2, id_split=i)(conv_2)
         ) for i in range(2)], mode='concat', concat_axis=1, name="conv_2")

    conv_3 = MaxPooling2D((3, 3), strides=(2, 2))(conv_2)
    conv_3 = cross_channel_normalization()(conv_3)
    conv_3 = ZeroPadding2D((1, 1))(conv_3)
    conv_3 = Conv2D(384, 3, activation='relu', name='conv_3')(conv_3)

    conv_4 = ZeroPadding2D((1, 1))(conv_3)
    conv_4 = merge([
        Conv2D(192, 3, activation="relu", name='conv_4_' + str(i + 1))(
            split_tensor(ratio_split=2, id_split=i)(conv_4)
        ) for i in range(2)], mode='concat', concat_axis=1, name="conv_4")

    conv_5 = ZeroPadding2D((1, 1))(conv_4)
    conv_5 = merge([
        Conv2D(128, 3, activation="relu", name='conv_5_' + str(i + 1))(
            split_tensor(ratio_split=2, id_split=i)(conv_5)
        ) for i in range(2)], mode='concat', concat_axis=1, name="conv_5")

    dense_1 = MaxPooling2D((3, 3), strides=(2, 2), name="convpool_5")(conv_5)

    dense_1 = Flatten(name="flatten")(dense_1)
    dense_1 = Dense(4096, activation='relu', name='dense_1')(dense_1)
    dense_2 = Dropout(0.5)(dense_1)
    dense_2 = Dense(4096, activation='relu', name='dense_2')(dense_2)
    dense_3 = Dropout(0.5)(dense_2)
    dense_3 = Dense(1000, name='dense_3')(dense_3)
    prediction = Activation("softmax", name="softmax")(dense_3)

    m = Model(input=inputs, output=prediction)

    if weights_path is None:
        weights_path = 'Data/alexnet_weights.h5'
    m.load_weights(weights_path)
    # Model was trained using Theano backend
    # This changes convolutional kernels from TF to TH, great accuracy improvement
    convert_all_kernels_in_model(m)

    # sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    # m.compile(optimizer=sgd, loss='mse')

    return m