示例#1
0
def train():
    video_dir = os.path.join(parentUrl, 'data', 'train', 'lab')
    box_to_vect_dataset = BoxToVectDataSet(
        train_set_file='data/train.npy',
        val_set_file='data/val.npy',
        video_files=[
            os.path.join(video_dir, '4p-c0.avi'),
            os.path.join(video_dir, '4p-c1.avi'),
            os.path.join(video_dir, '4p-c2.avi'),
            os.path.join(video_dir, '4p-c3.avi'),
        ],
        test_set_file=None,
        image_width=Config['image_width'],
        image_height=Config['image_height'])
    box_to_vect = Resnet(
        image_height=Config['image_height'],
        image_width=Config['image_width'],
        vector_dim=128,
        alpha=Config['alpha'],
        feature_map_layer='block_layer3',
        resnet_size=18,
        data_format='channels_first',
        mode='train',
        init_learning_rate=0.001,
        optimizer_name='adam',
        batch_size=Config['batch_size'],
        max_step=Config['max_step'],
        model_path='model/',
        logdir='log/',
    )

    with tf.Session() as sess:
        box_to_vect.train(box_to_vect_dataset, sess)
def classify_train(name, learning_rate, init_weight=None):
    print('start classify_train')
    net = Resnet()
    #net = SimpleVgg()
    model = net.get_model(learning_rate)
    if not init_weight == None:
        model.load_weights(init_weight)
    model.summary()
    generator = DataGenerator(name=name)
    run = '{}-{}-{}'.format(name,
                            time.localtime().tm_hour,
                            time.localtime().tm_min)
    log_dir = CLASSIFY_LOG_DIR.format(run)
    check_point = log_dir + '/' + name + '_checkpoint-{epoch:02d}-{val_loss:.4f}.hdf5'

    print("classify train round {}".format(run))
    tensorboard = TensorBoard(log_dir=log_dir, write_graph=False)
    checkpoint = ModelCheckpoint(filepath=check_point,
                                 monitor='val_loss',
                                 verbose=1,
                                 save_best_only=True)
    early_stopping = EarlyStopping(monitor='val_loss',
                                   patience=TRAIN_EARLY_STOPPING,
                                   verbose=1)

    model.fit_generator(
        generator.flow_classfication(mode='train'),
        steps_per_epoch=TRAIN_STEPS_PER_EPOCH,
        validation_data=generator.flow_classfication(mode='val'),
        validation_steps=TRAIN_VALID_STEPS,
        epochs=TRAIN_EPOCHS,
        verbose=1,
        callbacks=[tensorboard, checkpoint, early_stopping])
  def build(self, hp):
    maxnorm = hp.Choice('maxnorm', values=self.hyperparam['maxnorm'])

    resnet_model = Resnet(input_shape=(self.seqlen, self.channels), norm_max=maxnorm)
    if self.pretrained_weights is not None:
      resnet_model.set_weights(self.pretrained_weights)
    inp = Input(shape=(self.seqlen, self.channels))
    enc_inp = resnet_model(inp)

    dense_units = hp.Int('preclassification', min_value = self.hyperparam['dense_units']['min'],\
                         max_value = self.hyperparam['dense_units']['max'], step = self.hyperparam['dense_units']['step'])
    dense_out = Dense(units = dense_units, activation='relu',
                 kernel_constraint=MaxNorm(maxnorm,axis=[0,1]),
                 bias_constraint=MaxNorm(maxnorm,axis=0),
                 kernel_initializer=glorot_uniform(seed=0))(enc_inp)
    dense_out = Dropout(rate=hp.Choice('dropout', values = self.hyperparam['dropout']))(dense_out)
    output = Dense(self.num_classes, activation='softmax',
                 kernel_constraint=MaxNorm(maxnorm,axis=[0,1]),
                 bias_constraint=MaxNorm(maxnorm,axis=0),
                 kernel_initializer=glorot_uniform(seed=0))(dense_out)
    model = Model(inputs=inp, outputs=output)

    model.compile(optimizer=Adam(lr=hp.Choice('lr', values = self.hyperparam['lr'])),
                  loss=focal_loss(), metrics=['accuracy', macro_f1])

    return model
示例#4
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'i:o:r:c:',
                                   ['images=', 'labels=', 'resize=', 'crop='])
    except getopt.GetoptError as err:
        print(err)
        sys.exit()

    ipath, lpath = '', ''
    resize, crop = [0, 0], [0, 0]

    for o, a in opts:
        if o in ('-i', '--images') and type(a) == str:
            ipath = a
        elif o in ('-l', '--labels') and type(a) == str:
            lpath = a
        elif o in ('-r', '--resize') and type(a) == int:
            resize = [a, a]
        elif o in ('-c', '-crop') and type(a) == int:
            crop = [a, a]
        else:
            assert False, 'unhandled option'

    res = Resnet()
    res.input(ipath=ipath, lpath=lpath, resize=resize, crop=crop)
    res.compile_model()
    res.callback()
    res.train()
示例#5
0
def predict():
    model = Resnet()
    # original saved file with DataParallel
    state_dict = torch.load('sgdr_rgb.pkl')
    # create new OrderedDict that does not contain `module.`
    new_state_dict = OrderedDict()
    for k, v in state_dict.items():
        name = k[7:]  # remove `module.`
        new_state_dict[name] = v
    # load params
    model.load_state_dict(new_state_dict)
    #model.load_state_dict(torch.load('best.pkl'))
    th = choose_th(model)
    handle(model, th)
示例#6
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'i:o:r:c:m:', ['inpath=','outpath=','resize=','crop=','modelpath='])
    except getopt.GetoptError as err:
        print(err) 
        sys.exit()

    ipath,opath = '',''
    resize,crop = [0,0],[0,0]
    loadpath = None

    for o, a in opts:
        if o in ('-i', '--inpath') and type(a)==str:
            ipath = a
        elif o in ('-o', '--outpath') and type(a)==str:
            opath = a 
        elif o in ('-r', '--resize') and type(a)==int:
            resize = [a,a]
        elif o in ('-c', '--crop') and type(a)==int:
            crop = [a,a]
        elif o in ('-m', '--modelpath') and type(a)==str:
            modelpath = a
        else:
            assert False, 'unhandled option'    
    
    res = Resnet()
    if loadpath != None:
        res.load_model(modelpath)
        
    res.input(ipath=ipath,lpath=None,resize=resize,crop=crop)
    res.test(opath)
示例#7
0
def experiment(num_blocks, conv, learning_rate, rate, epochs, name):
    global net
    print 'Resnet ' + `num_blocks` + '-blocks, conv=' + conv.__name__ + ', learning rate=' + `learning_rate` + ', rate=' + `rate` + '\n'
    tf.reset_default_graph()
    net = None
    net = Resnet(n=num_blocks, conv=conv, rate=rate)
    run_session(net, name, learning_rate, epochs)
    print ''
示例#8
0
 def __init__(self):
     super(TripletNet, self).__init__()
     self.backbone = Resnet(mode='resnet34')
     self.pool = torch.nn.AdaptiveAvgPool2d(output_size=(1, 1))
     self.fc = torch.nn.Linear(in_features=512,
                               out_features=128,
                               bias=False)
     self.dropout = torch.nn.Dropout(p=0.2)
示例#9
0
def predict_test_only_classification():
    net = Resnet()
    name = 'Resnet'
    model = net.get_model()
    model.load_weights(CLASS_MODEL_PATH)

    columns = [
        'seriesuid', 'coordX', 'coordY', 'coordZ', 'class', 'probability'
    ]
    df = pd.DataFrame(columns=columns)

    #Use two round to detect. same step,different start point
    print('Round 1')
    df = predict_box(np.array([16, 16, 16]), model, columns, df)
    print('Round 2')
    df = predict_box(np.array([0, 0, 0]), model, columns, df)

    df.to_csv('./output/result_only_class.csv', index=False)
示例#10
0
文件: main.py 项目: ximzzzzz/SDAMS
def main():
    args = parse_args()
    if args is None:
        exit()

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        cnn = Resnet(sess,args)

        cnn.build_model()
        show_all_variables()

        if args.phase=='train':
            cnn.train()
            print('Train finished! \n')

            cnn.test()
            print('Test finished!')

        if args.phase=='test':
            cnn.test()
            print('Test finished!!')
示例#11
0
def main(argv):
    infile = argv[0]
    outfile = argv[1]

    seqlen = 1500
    channels = 6
    maxnorm = 20.0

    # Create model
    resnet_model = Resnet(input_shape=(seqlen, channels), norm_max=maxnorm)
    samp1 = Input(shape=(seqlen, channels))
    enc_samp1 = resnet_model(samp1)
    samp2 = Input(shape=(seqlen, channels))
    enc_samp2 = resnet_model(samp2)
    diff_layer = Lambda(lambda tensors: K.abs(tensors[0] - tensors[1]))
    diff_enc = diff_layer([enc_samp1, enc_samp2])

    dense_out = Dense(50,
                      activation='relu',
                      kernel_constraint=MaxNorm(maxnorm, axis=[0, 1]),
                      bias_constraint=MaxNorm(maxnorm, axis=0),
                      kernel_initializer=glorot_uniform(seed=0))(diff_enc)
    dense_out = Dropout(rate=0.2)(dense_out)
    output = Dense(1,
                   activation='sigmoid',
                   kernel_constraint=MaxNorm(maxnorm, axis=[0, 1]),
                   bias_constraint=MaxNorm(maxnorm, axis=0),
                   kernel_initializer=glorot_uniform(seed=0))(dense_out)
    model = Model(inputs=[samp1, samp2], outputs=output)
    model.load_weights(infile)
    for layer in model.layers:
        if layer.name == "model":
            resnet_model.set_weights(layer.get_weights())
            resnet_model.save_weights(outfile)
示例#12
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--data", help="cifar data path", default="../data/cifar-10-batches-py")
    parser.add_argument("--epochs", type=int, help="number of learning epoch, default is 10", default=10)
    parser.add_argument("--saving", help="wheter saving or not(each verbose iteration)", action="store_true")
    parser.add_argument("--batch_size", type=int, help="batch size(default is 32)", default=32)
    parser.add_argument("--verbose", type=int, help="verbosity cycle(default is 1 epoch)", default=1)
    parser.add_argument("--no_tqdm", help="whether to use tqdm process bar", action="store_true")
    parser.add_argument("--lr", type=float, help="learning rate, default is 0.001", default=1e-3)

    args = parser.parse_args()
    dirname = args.data

    X_train, y_train = prerprocess_train(dirname)
    X_test, y_test = prerprocess_test(dirname)

    device = 'gpu:0' if tfe.num_gpus() > 0 else 'cpu:0'
    resnet_model = Resnet(learning_rate=args.lr, device_name=device)
    resnet_model(tf.convert_to_tensor(X_train[:1]), True)
    resnet_model.summary()
    # resnet_model.load()  # you can load the latest model you saved

    if args.no_tqdm:
        tqdm_option = None
    else:
        tqdm_option = "normal"
    resnet_model.fit(X_train, y_train, X_test, y_test, epochs=args.epochs, verbose=args.verbose,
                     batch_size=args.batch_size, saving=args.saving, tqdm_option=tqdm_option)
示例#13
0
def create_resnet_model(seqlen, num_channels, maxnorm, modeldir, dense_units,
                        num_classes):
    resnet_model = Resnet(input_shape=(seqlen, num_channels), norm_max=maxnorm)
    # Load weights from pretrained model
    resnet_model.load_weights(os.path.join(modeldir, 'pretrained_resnet.h5'))
    samp = Input(shape=(seqlen, num_channels))
    enc_samp = resnet_model(samp)
    dense_out = Dense(dense_units,
                      activation='relu',
                      kernel_constraint=MaxNorm(maxnorm, axis=[0, 1]),
                      bias_constraint=MaxNorm(maxnorm, axis=0),
                      kernel_initializer=glorot_uniform(seed=0),
                      name='FC1')(enc_samp)
    dense_out = Dropout(rate=0.2)(dense_out)
    output = Dense(num_classes,
                   activation='softmax',
                   kernel_constraint=MaxNorm(maxnorm, axis=[0, 1]),
                   bias_constraint=MaxNorm(maxnorm, axis=0),
                   kernel_initializer=glorot_uniform(seed=0),
                   name='output')(dense_out)
    model = Model(inputs=samp, outputs=output)
    return model
示例#14
0
    def __init__(self, arch_cnn='densenet'):
        super(discriminator, self).__init__()

        if arch_cnn.lower() == 'densenet':
            self.cnn_backbone = Densenet()
            print('|------Initiating Discriminator: Densenet ------|')
        if arch_cnn.lower() == 'resnet':
            self.cnn_backbone = Resnet()
            print('|------Initiating Discriminator: Resnet ------|')

        self.l2_norm = l2_normalize_embedding()

        self.classifier = nn.Sequential(nn.Linear(2000, 4000, bias=True),
                                        nn.ReLU(), nn.Linear(4000,
                                                             2,
                                                             bias=True))

        self.classifier_2 = nn.Sequential(nn.Linear(1000, 2000, bias=True),
                                          nn.ReLU(),
                                          nn.Linear(2000, 27, bias=True))
示例#15
0
def update_learning_rate(current_lr, optimizer):
  new_lr = current_lr/10
  for g in optimizer.param_groups:
    g['lr'] = new_lr
  return new_lr



if __name__ == '__main__':
    use_cuda = len(sys.argv) > 1 and sys.argv[1] == 'cuda'
    num_epochs = 80
    # get cifar 10 data
    trainloader, testloader = get_dataset()
    benchmark, debug = False, True
    resnet = Resnet(n=2,dbg=debug)
    resnet.train()
    if use_cuda:
        resnet = resnet.cuda()
        for block in resnet.residual_blocks:
            block.cuda()
    current_lr = 1e-4
#     optimizer = optim.SGD(resnet.parameters(), lr=current_lr, weight_decay=0.0001, momentum=0.9)
    optimizer = optim.Adam(resnet.parameters(), lr=1e-4, weight_decay=0.0001)
    train_accs, test_accs = [], []
    gradient_norms = []
    def train_model():
      current_lr=1e-4
      stopping_threshold, current_count = 3, 0
      n_iters = 0
      for e in range(num_epochs):
示例#16
0
def main(argv):
    indir = args.indir
    outdir = args.outdir

    if not os.path.exists(outdir):
        os.makedirs(outdir)

    dirstr = 'lr{:.4f}-maxnorm{:.2f}-batchsize{:d}'.format(
        args.lr, args.maxnorm, args.batchsize)
    resultdir = os.path.join(outdir, dirstr)
    if not os.path.exists(resultdir):
        os.makedirs(resultdir)

    # Hyperparameters
    lr = args.lr  # learning rate
    num_epochs = args.num_epochs
    batch_size = args.batchsize

    # Read train data
    ftrain = h5py.File(os.path.join(args.indir, 'train_dataset.h5'), 'r')
    train_samples1 = ftrain['samp1']
    train_samples2 = ftrain['samp2']
    train_labels = np.array(ftrain['label'], dtype=np.int32)
    [num_train, seqlen, channels] = train_samples1.shape

    # Read validation data
    fval = h5py.File(os.path.join(args.indir, 'val_dataset.h5'), 'r')
    val_samples1 = fval['samp1']
    val_samples2 = fval['samp2']
    val_labels = np.array(fval['label'], dtype=np.int32)
    [num_val, seqlen, channels] = val_samples1.shape

    # Read test data
    ftest = h5py.File(os.path.join(args.indir, 'test_dataset.h5'), 'r')
    test_samples1 = ftest['samp1']
    test_samples2 = ftest['samp2']
    test_labels = np.array(ftest['label'], dtype=np.int32)
    [num_test, seqlen, channels] = test_samples1.shape

    # Data generators for train/val/test
    train_gen = DataGenerator(train_samples1, train_samples2, train_labels,\
                              batch_size=batch_size, seqlen=seqlen, channels=channels,\
                              shuffle=True, balance=True, augment=False, aug_factor=0.25)
    val_gen = DataGenerator(val_samples1, val_samples2, val_labels,\
                            batch_size=batch_size, seqlen=seqlen, channels=channels)
    test_gen = DataGenerator(test_samples1, test_samples2, test_labels,\
                             batch_size=batch_size, seqlen=seqlen, channels=channels)

    # Create model
    resnet_model = Resnet(input_shape=(seqlen, channels),
                          norm_max=args.maxnorm)
    samp1 = Input(shape=(seqlen, channels))
    enc_samp1 = resnet_model(samp1)
    samp2 = Input(shape=(seqlen, channels))
    enc_samp2 = resnet_model(samp2)
    diff_layer = Lambda(lambda tensors: K.abs(tensors[0] - tensors[1]))
    diff_enc = diff_layer([enc_samp1, enc_samp2])

    dense_out = Dense(50,
                      activation='relu',
                      kernel_constraint=MaxNorm(args.maxnorm, axis=[0, 1]),
                      bias_constraint=MaxNorm(args.maxnorm, axis=0),
                      kernel_initializer=glorot_uniform(seed=0))(diff_enc)
    dense_out = Dropout(rate=0.2)(dense_out)
    output = Dense(1,
                   activation='sigmoid',
                   kernel_constraint=MaxNorm(args.maxnorm, axis=[0, 1]),
                   bias_constraint=MaxNorm(args.maxnorm, axis=0),
                   kernel_initializer=glorot_uniform(seed=0))(dense_out)
    model = Model(inputs=[samp1, samp2], outputs=output)

    model.compile(optimizer=Adam(lr=lr),
                  loss=BinaryCrossentropy(),
                  metrics=['accuracy'])

    # Train model
    # Use early stopping and model checkpoints to handle overfitting and save best model
    model_checkpt = ModelCheckpoint(os.path.join(resultdir,'{epoch:02d}-{val_accuracy:.4f}.h5'),\
                                                 monitor='val_accuracy')#,\
    #mode='max', save_best_only=True)
    batch_renorm_cb = BatchRenormScheduler(
        len(train_gen))  # Implement batchrenorm after 1st epoch
    history = model.fit(train_gen,
                        epochs=num_epochs,
                        validation_data=val_gen,
                        verbose=1,
                        shuffle=False,
                        callbacks=[batch_renorm_cb, model_checkpt],
                        workers=2,
                        max_queue_size=20,
                        use_multiprocessing=False)

    # Plot training history
    plot_results(history.history['loss'], history.history['val_loss'],\
                 os.path.join(resultdir,'loss.jpg'), metric='Loss')
    plot_results(history.history['accuracy'], history.history['val_accuracy'],\
                 os.path.join(resultdir,'accuracy.jpg'), metric='Accuracy')

    # Predict probability on validation data using best model
    best_model_file, epoch, val_accuracy = get_best_model(resultdir)
    print(
        'Predicting with model saved at Epoch={:d} with val_accuracy={:0.4f}'.
        format(epoch, val_accuracy))
    model.load_weights(os.path.join(resultdir, best_model_file))
    probs = model.predict(test_gen)
    y_pred = probs.argmax(axis=1)
    y_true = test_labels
    test_acc = accuracy_score(y_true, y_pred)
    print('Test accuracy = {:0.2f}'.format(test_acc * 100.0))
示例#17
0
    categorical_ce_beta_regularized(ds.num_classes, eta=ETA)
}
LOSS = loss_options[args.loss]
METRICS = ['accuracy']
BATCH_SIZE = args.batch
EPOCHS = 100

for fold in range(args.fold, ds.n_folds):
    ds.set_fold(fold)

    # Remove old graph and create a new one keeping the same seeds
    reset_keras(1)

    # Create resnet model
    l_in = keras.layers.Input(ds.sample_shape)
    output = Resnet(l_in)
    output = keras.layers.Flatten()(output)

    # Create model output
    if args.output == 'softmax':
        output = keras.layers.Dense(
            ds.num_classes,
            kernel_initializer=keras.initializers.he_normal(),
            bias_initializer=keras.initializers.Constant(0))(output)
        output = keras.layers.Softmax()(output)
    elif args.output == 'stick':
        output = stick_breaking_layers(output, ds.num_classes)

    model = keras.models.Model(l_in, output, name='Resnet')

    # Compile the model
示例#18
0
            height=224,
            width=224)
        tr_data = ImageNetGenerator(hparams)
        tr_iterator = tr_data.get_iterator()

        hparams.txt_file = "dev.txt"
        hparams.is_training = False
        hparams.shuffle = False
        dev_data = ImageNetGenerator(hparams)
        dev_iterator = dev_data.get_iterator()

    X = tf.placeholder(tf.float32, [None, 224, 224, 3])
    y = tf.placeholder(tf.float32, [None, hparams.num_classes])
    is_training = tf.placeholder(tf.bool)
    learning_rate = tf.Variable(0.0, trainable=False)
    model = Resnet(is_training)
    y_ = model.create_model(X)
    loss, loss_summary = model.cross_entropy(y_, y)
    train_op, gradient_summary = model.optimizer(loss, learning_rate)
    accuracy, accuracy_summary = model.accuracy(y_, y)
    train_summary = tf.summary.merge(loss_summary + gradient_summary +
                                     accuracy_summary)

    saver = tf.train.Saver()
    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state('./ckpt')
        if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
            print("Reading model parameters from %s" %
                  ckpt.model_checkpoint_path)
            saver.restore(sess, ckpt.model_checkpoint_path)
        else:
示例#19
0
from examples.mt_model import Mt_model
from resnet import Resnet

network = Resnet()
model = Mt_model(Network=network,
                 ckpt_path='a',
                 tsboard_path='n',
                 x_shape=[None, 86, 1])
示例#20
0
        return opt_img
        
        
  
if __name__ == '__main__':

    transform = transforms.Compose([transforms.RandomResizedCrop(224),transforms.ToTensor() ,transforms.Normalize((0.5 , 0.5 , 0.5) , (0.5 , 0.5 , 0.5))])  
#    trainset = torchvision.datasets.CIFAR10(root = './data' , train = True , download = True , transform = transform)
#    trainloader = torch.utils.data.DataLoader(trainset , batch_size = 256 , shuffle = True , num_workers =2)    
    testset = torchvision.datasets.CIFAR10(root = './data' , train = False , download = True , transform = transform)
    testloader = torch.utils.data.DataLoader(testset , batch_size = 9 , shuffle = False , num_workers = 2)
#    classes = ('plane' , 'car' , 'bird' , 'cat' , 'deer' , 'dog' , 'frog' , 'horse' , 'ship' , 'truck')
    os.environ['CUDA_VISIBLE_DEVICES'] = "3"
#    
#    
    resnet = Resnet()
    device_ids=[0]
    resnet = resnet.cuda(device_ids[0])
    net=torch.nn.DataParallel(resnet,device_ids=device_ids)
#
    # read model
    print('===> Try load checkpoint')
    if os.path.isdir('checkpoint'):
        try:
            checkpoint = torch.load('./checkpoint/resnet_final.t7')
            net.load_state_dict(checkpoint['state'])        
            start_epoch = checkpoint['epoch']
            print('===> Load last checkpoint data')
        except FileNotFoundError:
            start_epoch = 0
            print('Can\'t found resnet_final.t7')
示例#21
0
    def __init__(self,
                 mode,
                 rpn_anchor_ratios,
                 rpn_anchor_scales,
                 mask_shape,
                 pool_size,
                 image_shape,
                 mini_mask_shape,
                 backbone_strides,
                 mean_pixel,
                 roi_size=7,
                 backbone='resnet50',
                 stage5=True,
                 norm='batch',
                 use_bias=True,
                 rpn_anchor_stride=1,
                 image_per_gpu=1,
                 gpu_count=1,
                 detection_max_instances=100,
                 train_rois_per_image=200,
                 num_classes=1,
                 use_mini_mask=True,
                 use_pretrained_model=True,
                 top_down_pyramid_size=256,
                 post_nms_rois_training=2000,
                 post_nms_rois_inference=1000,
                 pre_nms_limit=6000,
                 rpn_nms_threshold=0.7,
                 use_rpn_rois=True,
                 model_dir=None,
                 optimizer_method='Adam',
                 learning_rate=0.001,
                 momentum=0.9,
                 weight_decay=0.0001,
                 image_min_dim=800,
                 image_max_dim=1024,
                 image_min_scale=0.0,
                 image_resize_mode='square',
                 max_gt_instances=100,
                 rpn_train_anchors_per_image=256):

        assert mode in ['training', 'inference']
        assert optimizer_method in ['Adam', 'SGD']

        tf.reset_default_graph()
        self.graph = tf.Graph()

        self.mode = mode
        self.rpn_anchor_ratios = rpn_anchor_ratios
        self.rpn_anchor_scales = rpn_anchor_scales
        self.mask_shape = mask_shape
        self.pool_size = pool_size
        self.image_shape = np.array(image_shape)
        self.mini_mask_shape = mini_mask_shape
        self.backbone_strides = backbone_strides
        self.mean_pixel = mean_pixel

        self.roi_size = roi_size
        self.backbone = backbone
        self.stage5 = stage5
        self.norm = norm
        self.use_bias = use_bias
        self.rpn_anchor_stride = rpn_anchor_stride
        self.image_per_gpu = image_per_gpu
        self.gpu_count = gpu_count
        self.detection_max_instances = detection_max_instances
        self.train_rois_per_image = train_rois_per_image
        self.num_classes = num_classes
        self.use_mini_mask = use_mini_mask
        self.use_pretrained_model = use_pretrained_model
        self.top_down_pyramid_size = top_down_pyramid_size
        self.post_nms_rois_training = post_nms_rois_training
        self.post_nms_rois_inference = post_nms_rois_inference
        self.pre_nms_limit = pre_nms_limit
        self.rpn_nms_threshold = rpn_nms_threshold
        self.use_rpn_rois = use_rpn_rois
        self.model_dir = model_dir
        self.optimizer_method = optimizer_method
        self.learning_rate = learning_rate
        self.momentum = momentum
        self.weight_decay = weight_decay
        self.image_min_dim = image_min_dim
        self.image_max_dim = image_max_dim
        self.image_min_scale = image_min_scale
        self.image_resize_mode = image_resize_mode
        self.max_gt_instances = max_gt_instances
        self.rpn_train_anchors_per_image = rpn_train_anchors_per_image

        self.image_meta_size = 1 + 3 + 3 + 4 + 1 + self.num_classes
        self.reuse = False
        self._anchor_cache = {}
        self.batch_size = self.gpu_count * self.image_per_gpu
        self.backbone_shape = utils.compute_backbone_shapes(
            self.backbone, self.backbone_strides, self.image_shape)
        self.num_anchors_per_image = len(self.rpn_anchor_ratios) * (
            self.backbone_shape[0][0] * self.backbone_shape[0][0] +
            self.backbone_shape[1][0] * self.backbone_shape[1][0] +
            self.backbone_shape[2][0] * self.backbone_shape[2][0] +
            self.backbone_shape[3][0] * self.backbone_shape[3][0] +
            self.backbone_shape[4][0] * self.backbone_shape[4][0])

        with self.graph.as_default():

            self.is_training = tf.placeholder_with_default(False, [])
            self.input_image = tf.placeholder(dtype=tf.float32,
                                              shape=[
                                                  None, self.image_shape[0],
                                                  self.image_shape[1],
                                                  self.image_shape[2]
                                              ],
                                              name='input_image')
            self.input_image_meta = tf.placeholder(
                dtype=tf.int32,
                shape=[None, self.image_meta_size],
                name='input_image_meta')

            if mode == 'training':
                self.input_rpn_match = tf.placeholder(
                    dtype=tf.int32,
                    shape=[None, self.num_anchors_per_image, 1],
                    name='input_rpn_match')
                self.input_rpn_boxes = tf.placeholder(
                    dtype=tf.float32,
                    shape=[None, self.rpn_train_anchors_per_image, 4],
                    name='input_rpn_boxes')
                self.input_gt_class_ids = tf.placeholder(
                    dtype=tf.int32,
                    shape=[None, self.max_gt_instances],
                    name='input_gt_class_ids')
                self.input_gt_boxes = tf.placeholder(
                    dtype=tf.float32,
                    shape=[None, self.max_gt_instances, 4],
                    name='input_gt_boxes')
                self.input_gt_boxes_normalized = utils.norm_boxes_graph(
                    self.input_gt_boxes,
                    tf.shape(self.input_image)[1:3])
                self.proposal_count = self.post_nms_rois_training
                if self.use_mini_mask:
                    self.input_gt_masks = tf.placeholder(
                        dtype=tf.bool,
                        shape=[
                            None, self.mini_mask_shape[0],
                            self.mini_mask_shape[1], self.max_gt_instances
                        ],
                        name='input_gt_mask')
                else:
                    self.input_gt_masks = tf.placeholder(
                        dtype=tf.bool,
                        shape=[
                            None, self.image_shape[0], self.image_shape[1],
                            self.max_gt_instances
                        ],
                        name='input_gt_mask')

            elif mode == 'inference':
                self.input_anchors = tf.placeholder(dtype=tf.float32,
                                                    shape=[None, None, 4],
                                                    name='input_anchors')
                self.proposal_count = self.post_nms_rois_inference

            self.resnet = Resnet(name='resnet',
                                 architecture=self.backbone,
                                 is_training=self.is_training,
                                 stage5=self.stage5,
                                 use_bias=self.use_bias)

            arg_scope = nets.resnet_v2.resnet_arg_scope()
            with slim.arg_scope(arg_scope):
                _, self.end_points = nets.resnet_v2.resnet_v2_50(
                    self.input_image,
                    num_classes=None,
                    is_training=self.is_training)

            self.fpn = FPN(name='fpn',
                           top_down_pyramid_size=self.top_down_pyramid_size,
                           use_bias=self.use_bias)

            self.rpn = RPN(name='rpn',
                           anchors_per_location=len(self.rpn_anchor_ratios),
                           anchor_stride=self.rpn_anchor_stride,
                           is_training=self.is_training,
                           use_bias=self.use_bias)
            self.proposal = ProposalLayer(self.pre_nms_limit,
                                          self.proposal_count,
                                          self.rpn_nms_threshold,
                                          self.image_per_gpu)
            self.pyramidRoiPooling = PyramidRoiPooling(
                name='PyramidRoiPooling', roi_size=self.roi_size)
            self.objDetection = ObjDetection(
                image_per_gpu=self.image_per_gpu,
                gpu_count=self.gpu_count,
                detection_max_instances=self.detection_max_instances)
            self.targetDetection = TargetDetection(
                mask_shape=self.mask_shape,
                image_per_gpu=self.image_per_gpu,
                train_rois_per_image=self.train_rois_per_image)
            self.fpnClassifier = FpnClassifier('FpnClassifier',
                                               pool_size=self.pool_size,
                                               num_classes=self.num_classes,
                                               is_training=self.is_training)
            self.fpnMask = FpnMask('FpnMask',
                                   num_classes=self.num_classes,
                                   is_training=self.is_training)
示例#22
0
    def __init__(self):
        super(DPNet, self).__init__()
        resnet = Resnet()
        self.layer0 = resnet.layer0
        self.layer1 = resnet.layer1
        self.layer2 = resnet.layer2
        self.layer3 = resnet.layer3
        self.layer4 = resnet.layer4

        self.nlb_paramid1 = DPM(256, sub_sample=False)
        self.nlb_paramid2 = DPM(256, sub_sample=False)

        self.reduce_layer4 = nn.Sequential(
            nn.Conv2d(2048, 256, kernel_size=1),
            nn.BatchNorm2d(256),
            nn.PReLU(),
        )

        self.reduce_layer3 = nn.Sequential(
            nn.Conv2d(1024, 256, kernel_size=1),
            nn.BatchNorm2d(256),
            nn.PReLU(),
        )
        self.reduce_layer2 = nn.Sequential(
            nn.Conv2d(512, 256, kernel_size=1),
            nn.BatchNorm2d(256),
            nn.PReLU(),
        )
        self.reduce_layer1 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=1),
            nn.BatchNorm2d(256),
            nn.PReLU(),
        )

        self.predict_dnlp1 = nn.Sequential(
            nn.Conv2d(256, 64, kernel_size=3, padding=1), nn.BatchNorm2d(64),
            nn.PReLU(), nn.Conv2d(64, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64), nn.PReLU(), nn.Conv2d(64, 1, kernel_size=1))

        self.predict4 = nn.Sequential(
            nn.Conv2d(257, 64, kernel_size=3, padding=1), nn.BatchNorm2d(64),
            nn.PReLU(), nn.Conv2d(64, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64), nn.PReLU(), nn.Conv2d(64, 1, kernel_size=1))

        self.predict3 = nn.Sequential(
            nn.Conv2d(257, 64, kernel_size=3, padding=1), nn.BatchNorm2d(64),
            nn.PReLU(), nn.Conv2d(64, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64), nn.PReLU(), nn.Conv2d(64, 1, kernel_size=1))

        self.predict2 = nn.Sequential(
            nn.Conv2d(257, 64, kernel_size=3, padding=1), nn.BatchNorm2d(64),
            nn.PReLU(), nn.Conv2d(64, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64), nn.PReLU(), nn.Conv2d(64, 1, kernel_size=1))

        self.predict1 = nn.Sequential(
            nn.Conv2d(257, 64, kernel_size=3, padding=1), nn.BatchNorm2d(64),
            nn.PReLU(), nn.Conv2d(64, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64), nn.PReLU(), nn.Conv2d(64, 1, kernel_size=1))

        for m in self.modules():
            if isinstance(m, nn.ReLU) or isinstance(m, nn.Dropout):
                m.inplace = True
示例#23
0

#loading the training and the test data
X_train,Y_train,X_test,Y_test,classes=load_dataset()
num_classes=len(classes)
x1,y1,c1=X_train.shape

# defining placeholder
X=tf.placeholder(tf.float32,[None,x1,y1,c1])
Y=tf.placeholder(tf.float32,[None,num_classes])

#making dataset iterator
ds=Dataset.from_tensor_slices((X,Y)).batch(BATCH_SIZE).repeat()
iterator=tf.make_initializable_iterator()
sess.run(iterator.initializer,feed_dict={X:X_train,Y:Y_train})
batch_x,batch_y=iterator.get_next()

Y_pred=Resnet(batch_x,num_classes)
loss_op=train_op(Y_pred,batch_y)
optimizer-train_op(loss,learning_rate):
accuracy=tf.reduce_mean(tf.cast(tf.equal(tf.argmax(Y_pred,1),tf.argmax(Y_pred,1)),tf.float32))
init=tf.global_variables_initializer()
sess.run(init)
num_batches=X_train.shape[0]/BATCH_SIZE
for epoch in num_epochs:
    for _ in num_batches:
        _,accuracy,loss,=sess.run([optimizer,accuracy,loss])
    if epoch%2==0:
        print('Accuracy after {1} epoch is {0} '.format(accuracy,epoch))
        
    
示例#24
0
aug_train = Augmentation(x_train, y_train, 14400)
x_train, y_train = aug_train.data_augmentation()
print('after augmentation shape')
print(x_train.shape, x_test.shape)

#feature_train = FeatureExtraction(x_train)
#feature_test = FeatureExtraction(x_test)
#gray = feature_train.to_grayscale()
#sat = feature_train.get_color_sat().reshape(-1,1)
#pix = feature_train.get_color_pix_r().reshape(-1,1)
#grad = feature_train.get_img_gradient()
#print(gray.shape, sat.shape, pix.shape, grad.shape)
#x_train = np.concatenate((feature_train.to_grayscale(), feature_train.get_color_sat().reshape(-1,1), feature_train.get_color_pix_r().reshape(-1,1), feature_train.get_img_gradient()),axis = 1)
#x_test = np.concatenate((feature_test.to_grayscale(), feature_test.get_color_sat().reshape(-1,1), feature_test.get_color_pix_r().reshape(-1,1), feature_test.get_img_gradient()),axis = 1)
#print(x_train.shape)
#print(np.concatenate((gray, sat, pix), axis =1).shape)
#x_train = np.concatenate((gray, grad, sat, pix), axis =1)
#print('after data engineering x_train shape, x_test shape')
#print(x_train.shape, x_test.shape)

#x_train, x_test = data_eng.to_standardize(x_train, x_test)
#print('std done')

#x_train,x_test = data_eng.to_PCA(x_train, x_test)
#print('pca done')

res = Resnet(x_train, x_test, y_train, y_test)
accurate = res.train()

print(accurate)
示例#25
0
文件: main.py 项目: rmorain/resnet
# Main
from resnet import Resnet
import numpy as np

data = np.array([[0, 1, 1], [1, 0, 0], [1, 1, 1]])  # Example test dataset
layers_count = 3  # The number of layers in the resnet
node_count = 3  # The number of nodes in each layer
resnet = Resnet(data, layers_count, node_count)  # Initialize a resnet
resnet.train()
示例#26
0
def init_model(args):
    source_train_loader = get_loader(name_dataset=args.source,
                                     batch_size=args.batch_size,
                                     train=True)
    target_train_loader = get_loader(name_dataset=args.target,
                                     batch_size=args.batch_size,
                                     train=True)

    source_evaluate_loader = get_loader(name_dataset=args.source,
                                        batch_size=args.batch_size,
                                        train=False)
    target_evaluate_loader = get_loader(name_dataset=args.target,
                                        batch_size=args.batch_size,
                                        train=False)

    n_classes = len(source_train_loader.dataset.classes)

    # ~ Paper : "We initialized the other layers with the parameters pre-trained on ImageNet"
    # check https://github.com/pytorch/vision/blob/master/torchvision/models/alexnet.py
    model = Resnet(31)
    # ~ Paper : The dimension of last fully connected layer (fc8) was set to the number of categories (31)
    # model.classifier[6] = nn.Linear(4096, n_classes)
    # # ~ Paper : and initialized with N(0, 0.005)
    # torch.nn.init.normal_(model.classifier[6].weight, mean=0, std=5e-3)
    #
    # # Initialize bias to small constant number (http://cs231n.github.io/neural-networks-2/#init)
    # model.classifier[6].bias.data.fill_(0.01)

    model = model.to(device=args.device)

    # ~ Paper : "The learning rate of fc8 is set to 10 times the other layers as it was training from scratch."
    # optimizer = torch.optim.SGD([
    #     # {'params': model.features.parameters()},
    #     # {'params': model.classifier[:6].parameters()},
    #     # # fc8 -> 7th element (index 6) in the Sequential block
    #     {'params': model.classifier[6].parameters(), 'lr': 10 * args.lr}
    # ], lr=args.lr, momentum=args.momentum)  # if not specified, the default lr is used
    optimizer = torch.optim.SGD(
        [
            {
                'params': model.base_network.parameters()
            },
            # {'params': model.bottleneck_layer.parameters(), 'lr': 10 * CFG['lr']},
            {
                'params': model.classifier_layer.parameters(),
                'lr': 10 * args.lr
            },
        ],
        lr=args.lr,
        momentum=args.momentum,
        weight_decay=5e-4)
    tracker = Tracker()

    for i in range(args.epochs):
        train(model, optimizer, source_train_loader, target_train_loader,
              tracker, args, i)
        evaluate(model, source_evaluate_loader, 'source', tracker, args, i)
        evaluate(model, target_evaluate_loader, 'target', tracker, args, i)

    # Save logged classification loss, coral loss, source accuracy, target accuracy
    torch.save(tracker.to_dict(), args.da_loss + "_log.pth")
    print("Final Evaluation\r")
    return evaluate(model, target_evaluate_loader, 'target', tracker, args, i)
示例#27
0
     elif args.model_type == 'ex_gradcam2':
         model = VGG_interpretable_gradcam2(num_classes=num_classe)
     else:
         model = VGGNet(num_classes=num_classe)
         #model = VGG_gradcam(num_classes=num_classe)
 elif args.model == 'resnet':
     if args.model_type == 'ex_atten':
         model = VGG_interpretable_atten(num_classes=num_classe)
     elif args.model_type == 'ex':
         model = Resnet_interpretable(num_classes=num_classe)
     elif args.model_type == 'ex_gradcam':
         model = Resnet_interpretable_gradcam(num_classes=num_classe)
     elif args.model_type == 'ex_gradcam2':
         model = VGG_interpretable_gradcam2(num_classes=num_classe)
     else:
         model = Resnet(num_classes=num_classe)
 elif args.model == 'mobilenet':
     if args.model_type == 'ex_atten':
         model = VGG_interpretable_atten(num_classes=num_classe)
     elif args.model_type == 'ex':
         model = VGG_interpretable(num_classes=num_classe)
     elif args.model_type == 'ex_gradcam':
         model = Mobile_interpretable_gradcam(num_classes=num_classe)
     elif args.model_type == 'ex_gradcam2':
         model = VGG_interpretable_gradcam2(num_classes=num_classe)
     else:
         model = MobileNet(num_classes=num_classe)
 elif args.model == 'alexnet':
     if args.model_type == 'ex_atten':
         model = VGG_interpretable_atten(num_classes=num_classe)
     elif args.model_type == 'ex':
示例#28
0
def predict_test(name='lung',
                 mode='test',
                 seg_model_path=SEG_LUNG_TRAIN_WEIGHT,
                 class_model_path=CLASS_LUNG_TRAIN_WEIGHT,
                 seg_thresh_hold=0.8,
                 limit=[0, 0]):
    detect_net = UNet()
    class_net = Resnet()

    detect_model = detect_net.get_model(0.1)
    detect_model.load_weights(seg_model_path)
    class_model = class_net.get_model(0.1)
    class_model.load_weights(class_model_path)

    columns = [
        'seriesuid', 'coordX', 'coordY', 'coordZ', 'class', 'probability'
    ]
    df = pd.DataFrame(columns=columns)
    for img, meta in get_files(name, mode):
        count = 0
        cubs = []
        cub_sizes = []
        for w in range(limit[0], img.shape[0] - limit[0], 32):
            for h in range(limit[1], img.shape[1] - limit[1], 32):
                for d in range(0, img.shape[2], 32):
                    if d + INPUT_DEPTH > img.shape[2]:
                        d = img.shape[2] - INPUT_DEPTH
                    if h + INPUT_HEIGHT > img.shape[1]:
                        h = img.shape[1] - INPUT_HEIGHT
                    if w + INPUT_WIDTH > img.shape[0]:
                        w = img.shape[0] - INPUT_WIDTH
                    cub = img[w:w + INPUT_WIDTH, h:h + INPUT_HEIGHT,
                              d:d + INPUT_DEPTH]

                    if np.all(cub == ZERO_CENTER):
                        continue

                    #batch_cub = cub[np.newaxis, ..., np.newaxis]
                    cubs.append(cub)
                    cub_sizes.append([w, h, d])
        for k in range(0, len(cub_sizes), 16):
            t = 16
            if k + 16 >= len(cub_sizes):
                t = len(cub_sizes) - k

            batch_cub = np.array(cubs[k:t + k])
            batch_cub_sizes = cub_sizes[k:t + k]

            batch_cub = batch_cub[..., np.newaxis]
            pre_y_batch = detect_model.predict(batch_cub)
            for k in range(pre_y_batch.shape[0]):
                pre_y = pre_y_batch[k, :, :, :, 0] > seg_thresh_hold
                #print('predicted pix:'+ str(np.sum(pre_y)))
                if np.sum(pre_y) > 0:
                    crops, crop_centers, diameter, bboxes = crop_for_class(
                        img, pre_y, np.array(batch_cub_sizes[k]))
                    #print('find:'+str(len(crop_centers)))
                    for i, center in enumerate(crop_centers):
                        crop = crops[i]
                        crop_cub = crop[np.newaxis, ..., np.newaxis]
                        class_type = class_model.predict(crop_cub)
                        class_type = class_type[0]
                        index = np.argmax(class_type)
                        if index > 0:
                            #print('Add one')
                            location = meta['origin'] + center
                            new_row = pd.DataFrame([[
                                meta['seriesuid'], location[0], location[1],
                                location[2], label_softmax_reverse[index],
                                class_type[index]
                            ]],
                                                   columns=columns)
                            df = df.append(new_row, ignore_index=True)
        df.to_csv('./output/predict_' + name + '_' + mode + '.csv',
                  index=False)
    print('finished')
示例#29
0
    else:
        continue_training = False
    
    for optimizer in optim_array:
        for p_value in p_values: 
            print('-'*40, optimizer, '-'*40)
            op = optim_params[optimizer]
            
            op['lr'] = op['lr']/(10**i)


            if optimizer == 'adamw' and dataset=='imagenet':
                op['weight_decay'] = 0.05 

            if optimizer is not 'adamw':
                model = Resnet(data_format='channels_last', classes=hp['classes'], wt_decay = op['weight_decay'])
            else:
                model = Resnet(data_format='channels_last', classes=hp['classes'], wt_decay  = 0)

            model._set_inputs(tf.zeros((batch_size, 32, 32, 3)))

            learning_rate = tf.train.exponential_decay(op['lr'], tf.train.get_global_step() * batch_size,
                                               hp['decay_after']*train_size, 0.1, staircase=True)

            logfile = 'log_'+str(p_value)+ '_' + dataset +'.csv'

            if(continue_training):
                load_model_filepath = 'model_'+str(p_value)+'_'  + dataset + '_epochs'+ str(continue_epoch)+'.h5'
                save_model_filepath = 'model_'+str(p_value)+'_'  + dataset + '_epochs'+ str(continue_epoch+epochs)+'.h5'
                model = load_model(load_model_filepath, model)
            else:
示例#30
0
def test(model_path='model/model-30000'):
    video_dir = os.path.join(parentUrl, 'data', 'train', 'lab')
    box_to_vect_dataset = BoxToVectDataSet(
        train_set_file='data/train.npy',
        val_set_file='data/val.npy',
        video_files=[
            os.path.join(video_dir, '4p-c0.avi'),
            os.path.join(video_dir, '4p-c1.avi'),
            os.path.join(video_dir, '4p-c2.avi'),
            os.path.join(video_dir, '4p-c3.avi'),
        ],
        test_set_file='data/test.npy',
        image_width=Config['image_width'],
        image_height=Config['image_height'])
    box_to_vect = Resnet(
        image_height=Config['image_height'],
        image_width=Config['image_width'],
        vector_dim=128,
        alpha=Config['alpha'],
        feature_map_layer='block_layer3',
        resnet_size=18,
        data_format='channels_first',
        mode='test',
        init_learning_rate=0.001,
        optimizer_name='adam',
        batch_size=Config['batch_size'],
        model_path='model/',
        logdir='log/',
    )

    sess = tf.Session()
    saver = tf.train.Saver()
    saver.restore(sess, model_path)
    print('Load weights form', model_path)
    max_ap = 0
    min_an = 100
    for _ in range(100):

        image_batch, box_batch, box_ind_batch = box_to_vect_dataset.get_data_batch(
            batch_size=Config['batch_size'], split='test')

        embedding = box_to_vect.inference(image_batch, box_batch,
                                          box_ind_batch, sess)
        embedding = np.reshape(embedding, [-1, 3, embedding.shape[-1]])
        anchor, positive, negative = embedding[:,
                                               0, :], embedding[:,
                                                                1, :], embedding[:,
                                                                                 2, :]
        # print('anchor\n', anchor)
        # print('positive\n', positive)
        # print('negative\n', negative)

        a_p_squared = np.sqrt(np.sum(np.square(anchor - positive), axis=1))
        a_n_squared = np.sqrt(np.sum(np.square(anchor - negative), axis=1))
        print('-' * 50)
        # print('a_p_squared:\n', a_p_squared)
        # print('a_n_squared:\n', a_n_squared)
        a_p_squared_max = np.max(a_p_squared)
        a_n_squared_min = np.min(a_n_squared)
        print('a_p_squared_max:', a_p_squared_max)
        print('a_n_squared_min:', a_n_squared_min)
        # print('diff:\n', a_p_squared-a_n_squared)
        if max_ap < a_p_squared_max:
            max_ap = a_p_squared_max
        if min_an > a_n_squared_min:
            min_an = a_n_squared_min
        print('max_ap', max_ap)
        print('min_an', min_an)