選擇模型訓練

def densenet121_model(img_rows, img_cols, color_type=1, nb_dense_block=4, growth_rate=48, nb_filter=64, reduction=0.5, dropout_rate=0.0, weight_decay=1e-4, num_classes=None):
    '''
    DenseNet 121 Model for Keras

    Model Schema is based on 
    https://github.com/flyyufelix/DenseNet-Keras

    ImageNet Pretrained Weights 
    Theano: https://drive.google.com/open?id=0Byy2AcGyEVxfMlRYb3YzV210VzQ
    TensorFlow: https://drive.google.com/open?id=0Byy2AcGyEVxfSTA4SHJVOHNuTXc

    # Arguments
        nb_dense_block: number of dense blocks to add to end
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters
        reduction: reduction factor of transition blocks.
        dropout_rate: dropout rate
        weight_decay: weight decay factor
        classes: optional number of classes to classify images
        weights_path: path to pre-trained weights
    # Returns
        A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(img_rows, img_cols, color_type), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(color_type, img_rows, img_cols), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6,12,24,16] # For DenseNet-121
    nb_layers = [1,2,3,2]
    # Initial convolution
    print('********************',img_input.shape)
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    print('********************',x.shape)
    x = Convolution2D(nb_filter, 7, 7, subsample=(2, 2), name='conv1', bias=False)(x)
    print('********************',x.shape)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    print('********************',x.shape)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    print('********************',x.shape)
    x = Activation('relu', name='relu1')(x)
    print('********************',x.shape)
    
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    print('********************',x.shape)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)
    print('********************',x.shape)
    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx+2
        x, nb_filter = dense_block(x, stage, nb_layers[block_idx], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay)
        print('********************',x.shape)
        # Add transition_block
        x = transition_block(x, stage, nb_filter, compression=compression, dropout_rate=dropout_rate, weight_decay=weight_decay)
       
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x, final_stage, nb_layers[-1], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv'+str(final_stage)+'_blk_bn')(x)
    x = Scale(axis=concat_axis, name='conv'+str(final_stage)+'_blk_scale')(x)
    x = Activation('tanh', name='tanh'+str(final_stage)+'_blk')(x)

    #x_fc = GlobalAveragePooling2D(name='pool'+str(final_stage))(x)
    print('/////////////////////////////////////////////',x.shape)
    x_fc = Conv2D(500, kernel_size=(7,7), activation='elu', strides=(1,1), padding='valid', name='conv3',
                     kernel_regularizer=regularizers.l2(0.01))(x)
    
    print('hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh',x_fc.shape)
    
    #x_fc = Dense(1000, name='fc6')(x_fc)
    x_fc = Activation('softmax', name='prob')(x_fc)

    model = Model(img_input, x_fc, name='densenet')

##    if K.image_dim_ordering() == 'th':
##      # Use pre-trained weights for Theano backend
##      weights_path = '/home/xm/桌面/雲量檢測/cnn_finetune-master/imagenet_models/imagenet_models/densenet121_weights_tf.h5'
##    else:
##      # Use pre-trained weights for Tensorflow backend
##      weights_path = '/home/xm/桌面/雲量檢測/cnn_finetune-master/imagenet_models/imagenet_models/densenet121_weights_tf.h5'

##    model.load_weights(weights_path, by_name=True)

    # Truncate and replace softmax layer for transfer learning
    # Cannot use model.layers.pop() since model is not of Sequential() type
    # The method below works since pre-trained weights are stored in layers but not in the model
    x_newfc = GlobalAveragePooling2D(name='pool'+str(final_stage))(x)
    x_newfc = Dense(num_classes, name='fc6')(x_newfc)
    
    x_newfc = Activation('softmax', name='prob')(x_newfc)

    model = Model(img_input, x_newfc)

    # Learning rate is changed to 0.001
    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
    print("Training........................................")

##    list_train_loss = []
##    list_test_loss = []
##    list_learning_rate = []
##
##    for e in range(nb_epoch):
##
##        if e == int(0.5 * nb_epoch):
##            K.set_value(model.optimizer.lr, np.float32(learning_rate / 10.))
##
##        if e == int(0.75 * nb_epoch):
##            K.set_value(model.optimizer.lr, np.float32(learning_rate / 100.))
##
##        split_size = batch_size
##        num_splits = X_train.shape[0] / split_size
##        arr_splits = np.array_split(np.arange(X_train.shape[0]), num_splits)
##
##        l_train_loss = []
##        start = time.time()
##
##        for batch_idx in arr_splits:
##
##            X_batch, Y_batch = X_train[batch_idx], Y_train[batch_idx]
##            train_logloss, train_acc = model.train_on_batch(X_batch, Y_batch)
##
##            l_train_loss.append([train_logloss, train_acc])
##
##        test_logloss, test_acc = model.evaluate(X_test,
##                                                Y_test,
##                                                verbose=0,
##                                                batch_size=64)
##        list_train_loss.append(np.mean(np.array(l_train_loss), 0).tolist())
##        list_test_loss.append([test_logloss, test_acc])
##        list_learning_rate.append(float(K.get_value(model.optimizer.lr)))
##        # to convert numpy array to json serializable
##        print('Epoch %s/%s, Time: %s' % (e + 1, nb_epoch, time.time() - start))
##
##        d_log = {}
##        d_log["batch_size"] = batch_size
##        d_log["nb_epoch"] = nb_epoch
##        d_log["optimizer"] = opt.get_config()
##        d_log["train_loss"] = list_train_loss
##        d_log["test_loss"] = list_test_loss
##        d_log["learning_rate"] = list_learning_rate
##
##        json_file = os.path.join('./log/experiment_log_image.json')
##        with open(json_file, 'w') as fp:
##            json.dump(d_log, fp, indent=4, sort_keys=True)


    return model


def conv_block(x, stage, branch, nb_filter, dropout_rate=None, weight_decay=1e-4):
    '''Apply BatchNorm, Relu, bottleneck 1x1 Conv2D, 3x3 Conv2D, and option dropout
        # Arguments
            x: input tensor 
            stage: index for dense block
            branch: layer index within each dense block
            nb_filter: number of filters
            dropout_rate: dropout rate
            weight_decay: weight decay factor
    '''
    eps = 1.1e-5
    conv_name_base = 'conv' + str(stage) + '_' + str(branch)
    relu_name_base = 'tanh' + str(stage) + '_' + str(branch)

    # 1x1 Convolution (Bottleneck layer)
    inter_channel = nb_filter * 4  
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_x1_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base+'_x1_scale')(x)
    x = Activation('tanh', name=relu_name_base+'_x1')(x)
    x = Convolution2D(inter_channel, 1, 1, name=conv_name_base+'_x1', bias=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    # 3x3 Convolution
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_x2_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base+'_x2_scale')(x)
    x = Activation('tanh', name=relu_name_base+'_x2')(x)
    x = ZeroPadding2D((1, 1), name=conv_name_base+'_x2_zeropadding')(x)
    x = Convolution2D(nb_filter, 3, 3, name=conv_name_base+'_x2', bias=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    return x


def transition_block(x, stage, nb_filter, compression=1.0, dropout_rate=None, weight_decay=1E-4):
    ''' Apply BatchNorm, 1x1 Convolution, averagePooling, optional compression, dropout 
        # Arguments
            x: input tensor
            stage: index for dense block
            nb_filter: number of filters
            compression: calculated as 1 - reduction. Reduces the number of feature maps in the transition block.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
    '''

    eps = 1.1e-5
    conv_name_base = 'conv' + str(stage) + '_blk'
    relu_name_base = 'tanh' + str(stage) + '_blk'
    pool_name_base = 'pool' + str(stage) 

    x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base+'_scale')(x)
    x = Activation('tanh', name=relu_name_base)(x)
    x = Convolution2D(int(nb_filter * compression), 1, 1, name=conv_name_base, bias=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

##    x = AveragePooling2D((2, 2), strides=(2, 2), name=pool_name_base)(x)
    x = AveragePooling2D((1, 1), strides=(1, 1), name=pool_name_base)(x)
    return x


def dense_block(x, stage, nb_layers, nb_filter, growth_rate, dropout_rate=None, weight_decay=1e-4, grow_nb_filters=True):
    ''' Build a dense_block where the output of each conv_block is fed to subsequent ones
        # Arguments
            x: input tensor
            stage: index for dense block
            nb_layers: the number of layers of conv_block to append to the model.
            nb_filter: number of filters
            growth_rate: growth rate
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            grow_nb_filters: flag to decide to allow number of filters to grow
    '''

    eps = 1.1e-5
    concat_feat = x

    for i in range(nb_layers):
        branch = i+1
        x = conv_block(concat_feat, stage, branch, growth_rate, dropout_rate, weight_decay)
        concat_feat = merge([concat_feat, x], concat_axis=concat_axis,mode='concat',name='concat_'+str(stage)+'_'+str(branch))

        if grow_nb_filters:
            nb_filter += growth_rate

    return concat_feat, nb_filter

if __name__ == '__main__':

    # Example to fine-tune on 3000 samples from Cifar10

    img_rows, img_cols = 28, 28 # Resolution of inputs
    channel = 4
    num_classes = 3
    batch_size = 16
    nb_epoch = 3
    time_start = time.time();

    # Load Cifar10 data. Please implement your own load_data() module for your own dataset
##    X_train, Y_train, X_valid, Y_valid = load_cifar10_data(img_rows, img_cols)
    
    f1 = open('/home/xm/桌面/論文精華/total_data.txt','rb')
    X_train,Y_train,X_valid,Y_valid  = pickle.load(f1)
    #X_train,Y_train,X_valid,Y_valid  = np.load('/home/xm/桌面/論文數據修改/dataset11000.npy')
    Y_train = np_utils.to_categorical(Y_train, num_classes)
    Y_valid = np_utils.to_categorical(Y_valid, num_classes)
    # Load our model

    model = densenet121_model(img_rows=img_rows, img_cols=img_cols, color_type=channel, num_classes=num_classes)

將數據導入模型

model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=50,f shuffle=True,
          verbose=1,validation_data=(X_valid, Y_valid),callbacks = [history])

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章