Beispiel #1
0
 def __init__(self, mode):
     img = np.zeros((1, 224, 224, 3), dtype=np.float32)
     resnet = ResNet()
     self.rgbmodel = resnet.resnet()
     #print( self.rgbmodel.predict(img))
     inception = Inception()
     self.flowmodel = inception.inception()
     img = np.zeros((1, 299, 299, 10), dtype=np.float32)
Beispiel #2
0
    def __init__(self):
        # loading the YAML configuration file
        with open("config.yaml", 'r') as stream:
            try:
                self.config = yaml.safe_load(stream)
            except yaml.YAMLError as exc:
                print(exc)

        self.current_time = datetime.datetime.now().strftime("%Y-%m-%d  %H.%M")
        if not os.path.exists("logs"):
            os.mkdir("logs")
        os.chdir("logs")  # change to logs directory
        # getting the custom logger
        self.logger_name = "resnets_" + self.current_time + "_.log"
        self.logger = self.get_loggers(self.logger_name)
        self.logger.info("Classification using ResNets!")
        self.logger.info("Current time: " + str(self.current_time))
        self.train_on_gpu = False
        DataGen.__init__(self, self.config, self.logger)
        ResNet.__init__(self, self.config, self.logger)
        os.chdir("..")  # change directory to base path
def build_model(args):
    from models.ResNet import ResNetCifar as ResNet
    from models.SSHead import ExtractorHead
    print('Building model...')
    if args.dataset[:7] == 'cifar10':
        classes = 10
    elif args.dataset == 'cifar7':
        if not hasattr(args, 'modified') or args.modified:
            classes = 7
        else:
            classes = 10

    if args.group_norm == 0:
        norm_layer = nn.BatchNorm2d
    else:

        def gn_helper(planes):
            return nn.GroupNorm(args.group_norm, planes)

        norm_layer = gn_helper

    net = ResNet(args.depth,
                 args.width,
                 channels=3,
                 classes=classes,
                 norm_layer=norm_layer).cuda()
    if args.shared == 'none':
        args.shared = None

    if args.shared == 'layer3' or args.shared is None:
        from models.SSHead import extractor_from_layer3
        ext = extractor_from_layer3(net)
        head = nn.Linear(64 * args.width, 4)
    elif args.shared == 'layer2':  # according to "shared", extract layers from
        from models.SSHead import extractor_from_layer2, head_on_layer2
        ext = extractor_from_layer2(net)
        head = head_on_layer2(net, args.width,
                              4)  # head can be test-time trained
    ssh = ExtractorHead(ext, head).cuda()

    if hasattr(args, 'parallel') and args.parallel:
        net = torch.nn.DataParallel(net)
        ssh = torch.nn.DataParallel(ssh)
    return net, ext, head, ssh
Beispiel #4
0
def main(config):

    # create tensorflow session
    session_config = tf.ConfigProto()
    session_config.gpu_options.per_process_gpu_memory_fraction = config.gpu_memory_fraction
    sess = tf.Session(config=session_config)
    # create an instance of the model
    model = ResNet(config)
    # create your data generator
    data = DataGenerator(config)

    # create tensorboard logger
    logger = Logger(sess, config)
    # create trainer and pass all the previous components to it
    trainer = ResnetTrainer(sess, model, data, config, logger)
    # train model
    print "Start training..."
    trainer.train()
    sess.close()
Beispiel #5
0

#    plt.show()

p = {
    "model": "ResNet",  # for title
    "img_size": 128,
    # "num_classes": NUM_CLASSES,
    # "freeze_layers": 0,
    "epochs": 20,
    "batch_size": 20,
}

#history, model = MLP(x_train, y_train, p)
#history, model = LeNet(x_train, y_train, p)
#history, model = AlexNet(x_train, y_train, p)
#history, model = InceptionNet(x_train, y_train, p)
#history, model = VGG(x_train, y_train, p)
history, model = ResNet(x_train, y_train, p)
# history, model = OurNet(x_train, y_train, p)

plot_history(history, p)

results = model.evaluate(x_test, y_test, batch_size=20)
print("test loss, test acc:", results)

from keras.models import load_model
model = load_model("model.h5")
results = model.evaluate(x_test, y_test, batch_size=20)
print("test loss, test acc:", results)
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    resBlock = ResBlock(3, 64)
    resBlock = resBlock.to(device)

    preActResBlock = PreAct_ResBlock(3, 64)
    preActResBlock = preActResBlock.to(device)

    poolResBlock = ResBlock(3, 64, pool=True)
    poolResBlock = poolResBlock.to(device)

    poolPreActResBlock = PreAct_ResBlock(3, 64, pool=True)
    poolPreActResBlock = poolPreActResBlock.to(device)

    resNet = ResNet()
    resNet = resNet.to(device)

    for img, label in ds:
        img = img.view(1, *img.size())
        op = resBlock(img)
        res_out_size = op.size()
        assert res_out_size == (1, 67, 96, 96)
        print('Residual Block tests passed')

        op = preActResBlock(img)
        res_out_size = op.size()
        assert res_out_size == (1, 67, 96, 96)
        print('Pre Activation Residual Block tests passed')

        op = poolResBlock(img)
# speeds_train_aug = ['v15v45_simgan']
# # cDCGAN
# dataset_aug = ['syn']
# speeds_train_aug = ['v15v45_cdcgan']
# # SIM
# dataset_aug = ['sim']
# speeds_train_aug = ['v20_30mm','v30_50mm']

acc_GBDT = np.zeros((repeats, 4))
acc_ResNet = np.zeros((repeats, 4))
acc_HT = np.zeros((repeats, 4))
acc_CWT = np.zeros((repeats, 4))
for baseline in baselines:
    # baeline model
    if baseline == 'ResNet':
        model = ResNet((10240, 1))
    elif baseline == 'CWT+ResNet':
        model = ResNet((10240, 1))
    elif baseline == 'HT+ResNet':
        model = ResNet_env((256, 1))
    elif baseline == 'GBDT':
        model = GradientBoostingClassifier(learning_rate=0.05,
                                           n_estimators=200)

    for n in range(repeats):
        # get the balanced data and label the data
        x_train, y_train = data4balance(speeds, dataset_train, baseline)
        x_train, x_test, y_train, y_test = train_test_split(
            x_train, y_train, test_size=0.2, random_state=rs_init)
        x_train_aug, y_train_aug = data4balance(speeds_train_aug, dataset_aug,
                                                baseline)
image_size = 32
if (args.source == 'usps'
        and args.target == 'mnist') or (args.source == 'mnist'
                                        and args.target == 'usps'):
    channels = 1
else:
    channels = 3
if (args.source == 'cifar'
        and args.target == 'stl') or (args.source == 'stl'
                                      and args.target == 'cifar'):
    classes = 9
else:
    classes = 10

print('==> Building model..')
net = ResNet(args.depth, args.width, classes=classes, channels=channels).cuda()
ext = extractor_from_layer3(net)

print('==> Preparing datasets..')
sc_tr_dataset, sc_te_dataset = prepare_dataset(args.source,
                                               image_size,
                                               channels,
                                               path=args.data_root)
sc_tr_loader = torchdata.DataLoader(sc_tr_dataset,
                                    batch_size=args.batch_size,
                                    shuffle=True,
                                    num_workers=4)
sc_te_loader = torchdata.DataLoader(sc_te_dataset,
                                    batch_size=args.batch_size,
                                    shuffle=False,
                                    num_workers=4)
Beispiel #9
0
    if args.optimizer == 'SGD':
        optimizer_factory = optimizer_setup(torch.optim.SGD,
                                            lr=learning_rate,
                                            momentum=0.9)
    elif args.optimizer == 'Adam':
        optimizer_factory = optimizer_setup(optim.Adam, lr=learning_rate)

    if args.model == 'CnnVanilla':
        model = CnnVanilla(num_classes=10)
    elif args.model == 'AlexNet':
        model = AlexNet(num_classes=10)
    elif args.model == 'VggNet':
        model = VggNet(num_classes=10)
    elif args.model == 'ResNet':
        model = ResNet(num_classes=10)
    elif args.model == 'IFT725Net':
        model = IFT725Net(num_classes=10)
    elif args.model == 'IFT725UNet':
        model = IFT725UNet(num_classes=4)
        args.dataset = 'acdc'

        train_set = HDF5Dataset('train',
                                hdf5_file,
                                transform=acdc_base_transform)
        test_set = HDF5Dataset('test',
                               hdf5_file,
                               transform=acdc_base_transform)
    elif args.model == 'UNet':
        if args.dataset == 'projetSession':
            model = UNet(num_classes=2, in_channels=3)
dataset_test_1 = ['osc']
dataset_test_2 = ['peak']
dataset_test_3 = ['per']
speeds = ['v35v75']
sample_size = 10240
repeats = 10
rs_init = 1

acc_GBDT = np.zeros((10, 5))
acc_ResNet = np.zeros((10, 5))
acc_HT = np.zeros((10, 5))
acc_CWT = np.zeros((10, 5))
for baseline in baselines:
    # baeline model
    if baseline == 'ResNet':
        model = ResNet((10240, 1))
    elif baseline == 'CWT+ResNet':
        model = ResNet((10240, 1))
    elif baseline == 'HT+ResNet':
        model = ResNet_env((256, 1))
    elif baseline == 'GBDT':
        model = GradientBoostingClassifier(learning_rate=0.05,
                                           n_estimators=200)

    for n in range(repeats):
        # get the balanced data and label the data
        x_train, y_train = data4balance(speeds, dataset_train, baseline)
        x_test_0, y_test_0 = readdata(speeds, 'Bad', dataset_test_0, baseline)
        x_test_1, y_test_1 = readdata(speeds, 'Good', dataset_test_1, baseline)
        x_test_2, y_test_2 = readdata(speeds, 'Good', dataset_test_2, baseline)
        x_test_3, y_test_3 = readdata(speeds, 'Good', dataset_test_3, baseline)
Beispiel #11
0
def launch_train(args):
    image_size = 32
    num_workers = 1
    if (args.source == 'usps'
            and args.target == 'mnist') or (args.source == 'mnist'
                                            and args.target == 'usps'):
        channels = 1
    else:
        channels = 3
    if (args.source == 'cifar'
            and args.target == 'stl') or (args.source == 'stl'
                                          and args.target == 'cifar'):
        classes = 9
    else:
        classes = 10

    print('==> Building model..')
    net = ResNet(args.depth, args.width, classes=classes,
                 channels=channels).cuda()
    ext = extractor_from_layer3(net)

    print('==> Preparing datasets..')
    sc_tr_dataset, sc_te_dataset = prepare_dataset(args.source,
                                                   image_size,
                                                   channels,
                                                   path=args.data_root)
    sc_tr_loader = torchdata.DataLoader(sc_tr_dataset,
                                        batch_size=args.batch_size,
                                        shuffle=True,
                                        num_workers=num_workers,
                                        pin_memory=True)
    sc_te_loader = torchdata.DataLoader(sc_te_dataset,
                                        batch_size=args.batch_size,
                                        shuffle=False,
                                        num_workers=num_workers,
                                        pin_memory=True)

    tg_tr_dataset, tg_te_dataset = prepare_dataset(args.target,
                                                   image_size,
                                                   channels,
                                                   path=args.data_root)
    tg_te_loader = torchdata.DataLoader(tg_te_dataset,
                                        batch_size=args.batch_size,
                                        shuffle=False,
                                        num_workers=num_workers,
                                        pin_memory=True)
    sstasks = parse_tasks(args, ext, sc_tr_dataset, sc_te_dataset,
                          tg_tr_dataset, tg_te_dataset)

    criterion = nn.CrossEntropyLoss().cuda()
    parameters = list(net.parameters())
    for sstask in sstasks:
        parameters += list(sstask.head.parameters())
    optimizer = optim.SGD(parameters,
                          lr=args.lr,
                          momentum=0.9,
                          weight_decay=5e-4)
    scheduler = torch.optim.lr_scheduler.MultiStepLR(
        optimizer, [args.milestone_1, args.milestone_2],
        gamma=0.1,
        last_epoch=-1)

    all_epoch_stats = []
    print('==> Running..')
    for epoch in range(1, args.nepoch + 1):
        print('Source epoch %d/%d lr=%.3f' %
              (epoch, args.nepoch, optimizer.param_groups[0]['lr']))
        print('Error (%)\t\tmmd\ttarget test\tsource test\tunsupervised test')

        epoch_stats = train(args, net, ext, sstasks, criterion, optimizer,
                            scheduler, sc_tr_loader, sc_te_loader,
                            tg_te_loader, epoch)
        all_epoch_stats.append(epoch_stats)
        torch.save(all_epoch_stats, args.outf + '/loss.pth')
        plot_all_epoch_stats(all_epoch_stats, args.outf)
Beispiel #12
0
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

kwargs = {'num_workers': 1, 'pin_memory': True} if device else {}

# Selecting which model to use
print("Please enter the number of one of the following models: \n AlexNet : 1 \n ResNet50 : 2 \n SENet : 3 \n VGG16 : 4")
val = input("Enter here: ")
found = 0

# if value is found then model is selected and while loop exits
while found != 1:
    if val == "1":
        model = AlexNet()
        found = 1
    elif val == "2":
        model = ResNet(BasicBlock, [3, 4, 6, 3])
        found = 1
    elif val == "3":
        model = SENet(BasicBlock, [2, 2, 2, 2])
        found = 1
    elif val == "4":
        model = VGG()
        found = 1
    else:
        val = input("Please enter again: ")


model = model.to(device)

# showing the image function
def imshow(img):
testX = testX.astype("float")

mean = np.mean(trainX, axis=0)
std = np.std(trainX, axis=0)
trainX = (trainX - mean) / std
testX = (testX - mean) / std


lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.fit_transform(testY)

aug = ImageDataGenerator(width_shift_range=0.3, height_shift_range=0.3, zoom_range=0.3, rotation_range=20,
                         shear_range=0.1, horizontal_flip=True, fill_mode="nearest")

model = ResNet.build(32, 32, 3, 10, (9, 9, 9), (128, 128, 256, 512), reg=5e-4)
multi_model = multi_gpu_model(model, gpus=2, cpu_merge=True)


if args["model"] is None:
    print("[INFO] compiling model...")
    opt = SGD(lr=BASE_LR, momentum=0.9)
else:
    print(f"[INFO] loading {args['model']}...")
    opt = SGD(lr=BASE_LR, momentum=0.9)
    multi_model.load_weights(args["model"], by_name=True)

multi_model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])

callbacks = [
    EpochCheckPoint(args["checkpoints"], every=1, model=model, start_at=args["start_epoch"]),
                                          shuffle=True,
                                          num_workers=0)

testset = torchvision.datasets.CIFAR10(root='./data',
                                       train=False,
                                       download=True,
                                       transform=transform_test)
testloader = torch.utils.data.DataLoader(testset,
                                         batch_size=batch_size,
                                         shuffle=False,
                                         num_workers=0)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
           'ship', 'truck')

model = ResNet(26, 10, bottleneck=False).to(device)  # ResNet-26
#model = ResNet(14, 10, bottleneck=False).to(device)  # ResNet-14
#model = nn.DataParallel(ResNet(26, 10, bottleneck=False)).to(device)  # ResNet-26 for multiple GPU, but there will be some small problems when loading the model

criterion = nn.CrossEntropyLoss()

#optimizer = optim.Adam(model.parameters())
optimizer = optim.SGD(model.parameters(), lr=0.1)

lr_scheduler = lr_step_policy(0.1, [150, 250, 320], 0.1, 0)

# start training
print("training start!")
time_start = time.time()
max_accuracy = 0
for epoch in range(350):
def main():
    # TODO
    # config.py

    # Hyper params

    SEED = 1

    BATCH_SIZE = 128
    NUM_OF_CLASS = 10
    LEARNING_RATE = 1e-1
    OPTMIZER = 'SGD'
    SCHEDULER = 'custom'
    NUM_TRAIN_DATA = 50000
    NUM_TEST_DATA = 10000
    EPOCH = 10000

    TR_STEPS_PER_EPOCH = NUM_TRAIN_DATA // BATCH_SIZE
    TE_STEPS_PER_EPOCH = NUM_TEST_DATA // BATCH_SIZE

    GRAPH = False  #True
    HIST_LOG = False  #True

    # Random seed
    tf.random.set_seed(SEED)

    # Data loard

    ds_name = 'cifar10'
    builder = tfds.builder(ds_name)

    tr_ds, te_ds = builder.as_dataset(split=['train', 'test'])

    tr_ds = tr_ds\
            .map(random_flip_and_crop)\
            .repeat()\
            .shuffle(NUM_TRAIN_DATA, reshuffle_each_iteration=True)\
            .batch(BATCH_SIZE)\
            .prefetch(tf.data.AUTOTUNE)
    te_ds = te_ds\
            .repeat()\
            .batch(BATCH_SIZE)\
            .prefetch(tf.data.AUTOTUNE)

    # Set model
    model = ResNet()

    if GRAPH:
        model.trace_graph([100, 32, 32, 3])

    # LR schedule
    if SCHEDULER:
        if SCHEDULER == 'cosine':
            LEARNING_RATE = tf.keras.optimizers.schedules.CosineDecay(
                LEARNING_RATE, decay_steps=TR_STEPS_PER_EPOCH * 5, alpha=1e-3)
        elif SCHEDULER == 'exponentioal':
            LEARNING_RATE = tf.keras.optimizers.schedules.ExponentialDecay(
                LEARNING_RATE, decay_steps=500, decay_rate=0.1, staircase=True)

    # Optimizer
    if OPTMIZER:
        if OPTMIZER == 'Adam' or OPTMIZER == 'ADAM' or OPTMIZER == 'adam':
            optimizer = tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE,
                                                 epsilon=1e-07)
        elif OPTMIZER == 'sgd' or OPTMIZER == 'SGD':
            optimizer = tf.keras.optimizers.SGD(learning_rate=LEARNING_RATE,
                                                momentum=0.9)

    # Loss function
    loss = tf.keras.losses.CategoricalCrossentropy()

    # Metric
    tr_accuracy = tf.keras.metrics.CategoricalAccuracy()
    te_accuracy = tf.keras.metrics.CategoricalAccuracy()

    # Log config
    current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    train_log_dir = 'logs/gradient_tape/' + current_time + '/train'
    test_log_dir = 'logs/gradient_tape/' + current_time + '/test'

    train_loop(
        "steps",  # log_freq,
        train_log_dir,
        test_log_dir,
        tr_ds,
        te_ds,
        model,
        loss,
        optimizer,
        tr_accuracy,
        te_accuracy,
        EPOCH,
        TR_STEPS_PER_EPOCH,
        TE_STEPS_PER_EPOCH,
        HIST_LOG,
        SCHEDULER)