예제 #1
0
def create_model(name, num_classes):
    if name == 'resnet34':
        model = models.resnet34(True)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
        nn.init.xavier_uniform(model.fc.weight)
        nn.init.constant(model.fc.bias, 0)
    elif name == 'resnet152':
        model = models.resnet152(True)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
        nn.init.xavier_uniform(model.fc.weight)
        nn.init.constant(model.fc.bias, 0)
    elif name == 'densenet121':
        model = models.densenet121(True)
        model.classifier = nn.Linear(model.classifier.in_features, num_classes)
        nn.init.xavier_uniform(model.classifier.weight)
        nn.init.constant(model.classifier.bias, 0)
    elif name == 'vgg11_bn':
        model = models.vgg11_bn(False, num_classes)
    elif name == 'vgg19_bn':
        model = models.vgg19_bn(True)
        model.classifier._modules['6'] = nn.Linear(model.classifier._modules['6'].in_features, num_classes)
        nn.init.xavier_uniform(model.classifier._modules['6'].weight)
        nn.init.constant(model.classifier._modules['6'].bias, 0)
    elif name == 'alexnet':
        model = models.alexnet(True)
        model.classifier._modules['6'] = nn.Linear(model.classifier._modules['6'].in_features, num_classes)
        nn.init.xavier_uniform(model.classifier._modules['6'].weight)
        nn.init.constant(model.classifier._modules['6'].bias, 0)
    else:
        model = Net(num_classes)

    return model
예제 #2
0
def train_alexnet_imagenet(classes=5):
    model = alexnet(input_shape=(3, 227, 227), nb_classes=1000, mean_flag=True)
    model.load_weights('alexnet_weights.h5')

    # add in own classes ( maybe not necessary)
    last_layer = model.layers[-3].output
    dense_classifier = Dense(5, activation='softmax',
                             name='me_dense')(last_layer)
    model = Model(inputs=model.input, outputs=dense_classifier)
    plot_model(model, to_file='alexnet', show_shapes=True)
    print(model.summary())

    # freezing

    # # 3rd Last
    # for layer in model.layers[:-22]:
    # 	layer.trainable = False

    # # 2nd Last
    # for layer in model.layers[:-20]:
    # 	layer.trainable = False

    # # Last
    # for layer in model.layers[:-14]:
    # 	layer.trainable = False

    return model
예제 #3
0
def train_shallow_alexnet_imagenet_FCN(classes=5, freeze_flag=None):
    model = alexnet(input_shape=(3, 227, 227), nb_classes=1000, mean_flag=True)
    model.load_weights('alexnet_weights.h5')

    # modify architecture
    last_conv_1 = model.layers[5].output
    conv_2 = Conv2D(256, (5, 5),
                    strides=(1, 1),
                    activation='relu',
                    name='conv_2',
                    kernel_initializer='he_normal',
                    bias_initializer='he_normal')(last_conv_1)
    conv_2 = MaxPooling2D((3, 3), strides=(2, 2))(conv_2)
    conv_2 = crosschannelnormalization(name="convpool_2")(conv_2)
    conv_2 = ZeroPadding2D((2, 2))(conv_2)

    conv_2 = Dropout(0.5)(conv_2)

    conv_activate = Conv2D(classes,
                           kernel_size=(1, 1),
                           strides=(1, 1),
                           activation='relu',
                           kernel_initializer='he_normal',
                           bias_initializer='he_normal',
                           name='conv_activate')(conv_2)
    conv_activate = GlobalAveragePooling2D(
        data_format='channels_first')(conv_activate)

    model = Model(inputs=model.input, outputs=conv_activate)
    plot_model(model, to_file='shallowalex_fcn', show_shapes=True)
    print(model.summary())

    return model
예제 #4
0
    def __init__(self, opt):
        super().__init__()
        model = models.alexnet(pretrained=opt['pretrained'])

        relu = ['1', '4', '7', '9', '11']
        for i in relu:
            model.features._modules[i] = nn.LeakyReLU(inplace=True)

        self.features = model.features

        self.classifier = nn.Sequential(
            nn.Conv2d(256,
                      128,
                      kernel_size=(3, 3),
                      stride=(1, 2),
                      padding=(1, 1)), nn.LeakyReLU(inplace=True),
            nn.Conv2d(128,
                      1,
                      kernel_size=(3, 3),
                      stride=(1, 2),
                      padding=(1, 1)), nn.LeakyReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(4, 10)), View(1), nn.Sigmoid())

        self.Nf = num_parameters(self.features)
        self.Nc = num_parameters(self.classifier)
        s = '[%s] Features parameters: %d, Classifier Parameters: %d' % (
            self.name, self.Nf, self.Nc)
        print(s)
예제 #5
0
def check_summary():
    def torch_summarize(model, show_weights=True, show_parameters=True):
        """Summarizes torch model by showing trainable parameters and weights."""
        from torch.nn.modules.module import _addindent

        tmpstr = model.__class__.__name__ + ' (\n'
        for key, module in model._modules.items():
            # if it contains layers let call it recursively to get params and weights
            if type(module) in [
                    torch.nn.modules.container.Container,
                    torch.nn.modules.container.Sequential
            ]:
                modstr = torch_summarize(module)
            else:
                modstr = module.__repr__()
            modstr = _addindent(modstr, 2)

            params = sum([np.prod(p.size()) for p in module.parameters()])
            weights = tuple([tuple(p.size()) for p in module.parameters()])

            tmpstr += '  (' + key + '): ' + modstr
            if show_weights:
                tmpstr += ', weights={}'.format(weights)
            if show_parameters:
                tmpstr += ', parameters={}'.format(params)
            tmpstr += '\n'

        tmpstr = tmpstr + ')'
        return tmpstr

    # Test
    import torchvision.models as models
    model = models.alexnet()
    print(torch_summarize(model))
예제 #6
0
def test_alexnet_imagenet(classes=5):
    model = alexnet(input_shape=(3, 227, 227), nb_classes=1000, mean_flag=True)
    model.load_weights('alexnet_weights.h5')

    model = Model(inputs=model.input, outputs=model.layers[-2].output)
    plot_model(model, to_file='alexnet', show_shapes=True)
    print(model.summary())

    return model
예제 #7
0
def create_model(name, num_classes):
    if name == 'resnet34':
        model = models.resnet34(True)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
        nn.init.xavier_uniform(model.fc.weight)
        nn.init.constant(model.fc.bias, 0)
    elif name == 'resnet50':
        model = models.resnet50(True)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
        nn.init.xavier_uniform(model.fc.weight)
        nn.init.constant(model.fc.bias, 0)
    elif name == 'resnet152':
        model = models.resnet152(True)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
        nn.init.xavier_uniform(model.fc.weight)
        nn.init.constant(model.fc.bias, 0)
    elif name == 'seresnet50':
        model = models.se_resnet50()
        model.last_linear = nn.Linear(model.last_linear.in_features,
                                      num_classes,
                                      bias=True)
    elif name == 'seresnet152':
        model = models.se_resnet152()
        model.last_linear = nn.Linear(model.last_linear.in_features,
                                      num_classes,
                                      bias=True)
    elif name == 'dpn131':
        model = models.dpn131()
        model.classifier = nn.Conv2d(2688,
                                     num_classes,
                                     kernel_size=1,
                                     bias=True)
    elif name == 'densenet121':
        model = models.densenet121(True)
        model.classifier = nn.Linear(model.classifier.in_features, num_classes)
        nn.init.xavier_uniform(model.classifier.weight)
        nn.init.constant(model.classifier.bias, 0)
    elif name == 'vgg11_bn':
        model = models.vgg11_bn(False, num_classes)
    elif name == 'vgg19_bn':
        model = models.vgg19_bn(True)
        model.classifier._modules['6'] = nn.Linear(
            model.classifier._modules['6'].in_features, num_classes)
        nn.init.xavier_uniform(model.classifier._modules['6'].weight)
        nn.init.constant(model.classifier._modules['6'].bias, 0)
    elif name == 'alexnet':
        model = models.alexnet(True)
        model.classifier._modules['6'] = nn.Linear(
            model.classifier._modules['6'].in_features, num_classes)
        nn.init.xavier_uniform(model.classifier._modules['6'].weight)
        nn.init.constant(model.classifier._modules['6'].bias, 0)
    else:
        model = Net(num_classes)

    return model
예제 #8
0
파일: predict.py 프로젝트: uo-a7/face-attr
def predict(path):
    print(path)
    pil_img = Image.open(path)
    input = transforms(pil_img)
    input = input[None]
    input = t.autograd.Variable(input)

    model = alexnet(num_classes=2)
    model.load(
        'checkpoints/custom_models.alexnet.AlexNet_epoch[99.100]_190415154539.pt'
    )
    model.eval()

    preds = F.softmax(model(input), dim=1)

    print(preds)
예제 #9
0
def train_shallow_alexnet_imagenet(classes=5, freeze_flag=None):
    model = alexnet(input_shape=(3, 227, 227), nb_classes=1000, mean_flag=True)
    model.load_weights('alexnet_weights.h5')

    # modify architecture
    # ##################### Ori #####################
    # last_conv_1 = model.layers[5].output
    # conv_2 = Conv2D(256, (5, 5), strides=(1, 1), activation='relu', name='conv_2', kernel_initializer='he_normal', bias_initializer='he_normal')(last_conv_1)
    # conv_2 = MaxPooling2D((3, 3), strides=(2, 2))(conv_2)
    # conv_2 = crosschannelnormalization(name="convpool_2")(conv_2)
    # conv_2 = ZeroPadding2D((2,2))(conv_2)

    # conv_2 = Flatten(name="flatten")(conv_2)
    # conv_2 = Dropout(0.5)(conv_2)
    # ##############################################

    ################# Use 2 conv with weights ######################
    conv_2 = model.layers[13].output
    conv_2 = Flatten(name='flatten')(conv_2)
    conv_2 = Dropout(0.5)(conv_2)
    ################################################################

    # ##### FC for experiments #####
    # fc_1 = Dense(4096, activation='relu', name='fc_1', kernel_initializer='he_normal', bias_initializer='he_normal')(conv_2)
    # fc_2 = Dense(4096, activation='relu', name='fc_2', kernel_initializer='he_normal', bias_initializer='he_normal')(fc_1)
    # ##############################

    ##### GAP experiments #####
    # gap = GlobalAveragePooling2D(data_format = 'channels_first')(conv_2)
    # gap = Dropout(0.5)(gap)
    ###########################

    dense_1 = Dense(classes,
                    kernel_initializer='he_normal',
                    bias_initializer='he_normal')(conv_2)
    prediction = Activation("softmax")(dense_1)

    model = Model(inputs=model.input, outputs=prediction)
    plot_model(model, to_file='shallowalex', show_shapes=True)
    print(model.summary())
    return model
예제 #10
0
def main(id, n_files):
    n_files = int(n_files)
    model = alexnet(WIDTH, HEIGHT, LR, BATCH_SIZE)
    train_data = load_data('data/balanced/balanced_data_%s.npy', 0)
    
    for i in range(EPOCHS):
        
        size_test = int(0.1*len(train_data))
        train = train_data[:-size_test]
        test = train_data[-size_test:]

        X = np.array([i[0] for i in train]).reshape(-1, WIDTH, HEIGHT, 1)
        Y = [i[1] for i in train]

        test_x = np.array([i[0]
                           for i in test]).reshape(-1, WIDTH, HEIGHT, 1)
        test_y = [i[1] for i in test]

        model.fit({'input': X}, {'targets': Y}, n_epoch=1, validation_set=({'input': test_x}, {'targets': test_y}),
                      snapshot_step=500, show_metric=True, run_id='model_%s_%s_%s' % ('alexnet', EPOCHS, id))

        model.save('model/model.tflearn')
예제 #11
0
HEIGHT = 300
LR = 1e-3
EPOCHS = 30

MODEL_NAME = 'alexnet_v1'
PREV_MODEL = ''

forward = [1, 0, 0, 0, 0, 0, 0]
back = [0, 1, 0, 0, 0, 0, 0]
left = [0, 0, 1, 0, 0, 0, 0]
right = [0, 0, 0, 1, 0, 0, 0]
forward_left = [0, 0, 0, 0, 1, 0, 0]
forward_right = [0, 0, 0, 0, 0, 1, 0]
noop = [0, 0, 0, 0, 0, 0, 1]

model = alexnet(WIDTH, HEIGHT, LR, output=7)

for e in range(EPOCHS):
    #data_order = [i for i in range(1,FILE_I_END+1)]
    data_order = [i for i in range(1, FILE_I_END + 1)]
    shuffle(data_order)
    try:
        file_name = "D:\\Projects\\SelfDrivingFZero\\trainingData-0.npy"
        # full file info
        train_data = np.load(file_name)
        shuffle(train_data)
        train = train_data[:-100]
        test = train_data[-100:]

        X = np.array([i[0] for i in train]).reshape(-1, WIDTH, HEIGHT, 1)
        Y = [i[1] for i in train]
import numpy as np
import models

WIDTH = 30
HEIGHT = 18
LR = 1e-3
EPOCH = 8
MODEL_NAME = 'Trex-{}-{}-{}-epochs.model'.format(LR, 'alexnet', EPOCH)

model = models.alexnet(WIDTH, HEIGHT, LR, 2)

train_data = np.load('training_data_v2.npy')

train = train_data[:-50]
test = train_data[-50:]

X = np.array([i[0] for i in train]).reshape(-1, WIDTH, HEIGHT, 1)
y = [i[1] for i in train]

test_X = np.array([i[0] for i in test]).reshape(-1, WIDTH, HEIGHT, 1)
test_y = [i[1] for i in test]

model.fit({'input': X}, {'targets': y},
          n_epoch=EPOCH,
          validation_set=({
              'input': test_X
          }, {
              'targets': test_y
          }),
          snapshot_step=50,
          show_metric=True,
예제 #13
0
import torch
num_classes = 18
inputs = torch.rand([1, 3, 224, 224])
test = models.resnet34(num_classes=num_classes, pretrained='imagenet')
assert test(inputs).size()[1] == num_classes
print('ok')
test = models.resnet50(num_classes=num_classes, pretrained='imagenet')
assert test(inputs).size()[1] == num_classes
print('ok')
test = models.resnet101(num_classes=num_classes, pretrained='imagenet')
assert test(inputs).size()[1] == num_classes
print('ok')
test = models.resnet152(num_classes=num_classes, pretrained='imagenet')
assert test(inputs).size()[1] == num_classes
print('ok')
test = models.alexnet(num_classes=num_classes, pretrained='imagenet')
assert test(inputs).size()[1] == num_classes
print('ok')
test = models.densenet121(num_classes=num_classes, pretrained='imagenet')
assert test(inputs).size()[1] == num_classes
print('ok')
test = models.densenet169(num_classes=num_classes, pretrained='imagenet')
assert test(inputs).size()[1] == num_classes
print('ok')
test = models.densenet201(num_classes=num_classes, pretrained='imagenet')
assert test(inputs).size()[1] == num_classes
print('ok')
test = models.densenet201(num_classes=num_classes, pretrained='imagenet')
assert test(inputs).size()[1] == num_classes
print('ok')
예제 #14
0
def main():
    parser = argparse.ArgumentParser(
        description='Deep Learning SNN simulation')
    parser.add_argument('--config-file',
                        help='.ini file specifying simulation params')
    args = parser.parse_args()
    print(args)

    config = configparser.ConfigParser()
    config.read(args.config_file)
    pprint.pprint(
        {section: dict(config[section])
         for section in config.sections()})
    print

    defaults = config['DEFAULT']
    device = defaults['device']
    app_name = defaults['app_name']

    print('[INFO] Simulating spiking {}'.format(app_name))
    if not os.path.isdir(app_name):
        os.mkdir(app_name)
    out_dir = app_name

    org_model = config['original model']
    num_classes = org_model.getint('num_classes')
    model_path = org_model['model_path']
    file_name = org_model['file_name']
    batch_size = org_model.getint('batch_size')
    class_num = org_model.getint('class')

    " Load the original model "
    net = None
    if 'vgg_cifar10' in org_model['arch']:
        from models import VGG_16_cifar10
        net = VGG_16_cifar10
    if 'mobnet_cif10_mod' in org_model['arch']:
        from models import MobileNet_mod
        net = MobileNet_mod()
    if 'mobnet_cif100' in org_model['arch']:
        from models import mobilenet_cif100
        net = mobilenet_cif100()
    if 'vgg_cif100' in org_model['arch']:
        from models import vgg13_bn
        net = vgg13_bn()
    if 'svhn' in org_model['arch']:
        from models import svhn
        net = svhn()
    if 'alex' in org_model['arch']:
        from models import alexnet
        net = alexnet()
    if 'lenet5' in org_model['arch']:
        from models import lenet5
        net = lenet5()

    print(net)
    state = None
    state, net = load_model(net, model_path, file_name)

    net = net.to(device)

    " Load the dataset "
    testloader, img_size = None, None
    data_config = config['dataset']
    data_dir = data_config['data_dir']
    if data_config['dataset'] == 'cifar10':
        trainloader, testloader = load_cifar10(data_dir, org_model['arch'],
                                               batch_size, class_num)
        img_size = (-1, 3, 32, 32)

    if data_config['dataset'] == 'imagenet':
        trainloader, testloader = load_imagenet(data_dir,
                                                batch_size,
                                                shuffle=False)
        img_size = (-1, 3, 224, 224)

    if data_config['dataset'] == 'cifar100':
        trainloader, testloader = load_cifar100(data_dir, org_model['arch'],
                                                batch_size, class_num)
        img_size = (-1, 3, 32, 32)

    if data_config['dataset'] == 'svhn':
        trainloader, testloader = load_svhn(data_dir, org_model['arch'],
                                            batch_size, class_num)
        img_size = (-1, 3, 32, 32)

    if data_config['dataset'] == 'mnist':
        trainloader, testloader = load_mnist(data_dir, org_model['arch'],
                                             batch_size, class_num)
        img_size = (-1, 1, 28, 28)

    " Tasks to do "
    tasks = config['functions']
    " validate original model "
    if tasks.getboolean('validate'):
        validate(net, testloader, device)

    " fold back BN layers if any "
    new_net = None
    remove_bn = tasks.getboolean('remove_bn')
    if remove_bn:
        if has_bn(net):
            if 'mobnet_cif10_mod' in org_model['arch']:
                from models import MobileNet_mod_nobn
                new_net = MobileNet_mod_nobn()
            elif 'mobnet_cif100' in org_model['arch']:
                from models import mobilenet_cif100_nobn
                new_net = mobilenet_cif100_nobn()
            elif 'vgg_cif100' in org_model['arch']:
                from models import vgg13_nobn
                new_net = vgg13_nobn()

            new_net = merge_bn(net, new_net)
            new_net = new_net.to(device)
            print(new_net)
            print('Validating model after folding back BN layers...')
            validate(new_net, testloader, device)
            save_model(new_net, state, out_dir, 'nobn_' + file_name)
        else:
            print('model has no BN layers')

    " use model with folded BN "
    use_nobn = tasks.getboolean('use_nobn')
    if use_nobn:
        if 'mobnet_cif10_mod' in org_model['arch']:
            from models import MobileNet_mod_nobn
            net = MobileNet_mod_nobn()
        elif 'mobnet_cif100' in org_model['arch']:
            from models import mobilenet_cif100_nobn
            net = mobilenet_cif100_nobn()
        elif 'vgg_cif100' in org_model['arch']:
            from models import vgg13_nobn
            net = vgg13_nobn()
        state, net = load_model(net, out_dir, 'nobn_' + file_name)
        net = net.to(device)

    " validate model with folded BN "
    validate_nobn = tasks.getboolean('validate_nobn')
    if not remove_bn and validate_nobn:
        if not has_bn(net):
            print('Validating no_bn model...')
            validate(net, testloader, device)
        else:
            print('model has BN layers!! Exiting..')
            exit()

    " compute thresholds "
    spike_config = config['spiking']
    percentile = spike_config.getfloat('percentile')
    if spike_config.getboolean('compute_thresholds'):
        compute_thresholds(net, testloader, out_dir, percentile, device)

    " convert ann to snn "
    thresholds, max_acts = None, None
    if spike_config.getboolean('convert_to_spike'):
        from spiking import createSpikingModel

        thresholds = np.loadtxt(os.path.join(out_dir, 'thresholds.txt'))
        max_acts = np.loadtxt(os.path.join(out_dir, 'max_acts.txt'))

        spike_net = createSpikingModel(net, org_model['arch'], num_classes, spike_config, \
                torch.from_numpy(thresholds), max_acts, device, out_dir)
        #print(spike_net.state_dict().keys())

        print(spike_net)

    " simulate snn "
    if spike_config.getboolean('simulate_spiking'):
        from spiking import simulate_spike_model

        thresholds = np.loadtxt(os.path.join(out_dir, 'thresholds.txt'))
        max_acts = np.loadtxt(os.path.join(out_dir, 'max_acts.txt'))

        thresholds = torch.from_numpy(thresholds).to(device)
        max_acts = torch.from_numpy(max_acts).to(device)

        sbi, model_partial = None, None
        # sbi: spike buffer index
        if spike_config.getboolean('hybrid_model'):
            from spiking import createSpikingModel, create_partial_model

            spike_net = createSpikingModel(net, org_model['arch'], num_classes, spike_config, \
                    thresholds, max_acts, device, out_dir)
            split_layer = spike_config.getint('split_layer')
            sbi, model_partial = create_partial_model(split_layer, net,
                                                      spike_net,
                                                      org_model['arch'])
            print(model_partial)

        simulate_spike_model(net, org_model['arch'], testloader, config, thresholds.float(), \
                max_acts, num_classes, img_size, sbi, model_partial, device)

    if class_num < 0:
        class_num = ''
    " plot correlations "
    if spike_config.getboolean('plot_correlations'):
        from spiking import plot_correlations

        corr = np.load(
            os.path.join(out_dir, 'layerwise_corr' + str(class_num) + '.npy'))
        plot_config = config['plotting']
        plot_correlations(corr, out_dir, plot_config, class_num)

    " plot activity "
    if spike_config.getboolean('plot_activity'):
        from spiking import plot_activity

        container = np.load(
            os.path.join(out_dir, 'layerwise_acts' + str(class_num) + '.npz'))
        max_acts = np.loadtxt(os.path.join(out_dir, 'max_acts.txt'))
        plot_activity(container, max_acts, out_dir, class_num)

    " plot histogram of spikes "
    if spike_config.getboolean('plot_histogram'):
        from spiking import plot_histogram

        container = np.load(
            os.path.join(out_dir, 'layerwise_acts' + str(class_num) + '.npz'))
        max_acts = np.loadtxt(os.path.join(out_dir, 'max_acts.txt'))
        plot_histogram(container, max_acts, spike_config, out_dir, class_num)
예제 #15
0
def train_multiclass(
        train_file,
        test_file,
        stat_file,
        model='mobilenet_v2',
        classes=('artist_name', 'genre', 'style', 'technique', 'century'),
        im_path='/export/home/kschwarz/Documents/Data/Wikiart_artist49_images',
        label_file='_user_labels.pkl',
        chkpt=None,
        weight_file=None,
        use_gpu=True,
        device=0,
        epochs=100,
        batch_size=32,
        lr=1e-4,
        momentum=0.9,
        log_interval=10,
        log_dir='runs',
        exp_name=None,
        seed=123):
    argvars = locals().copy()
    torch.manual_seed(seed)

    # LOAD DATASET
    with open(stat_file, 'r') as f:
        data = pickle.load(f)
        mean, std = data['mean'], data['std']
        mean = [float(m) for m in mean]
        std = [float(s) for s in std]
    normalize = transforms.Normalize(mean=mean, std=std)
    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(90),
        transforms.ToTensor(),
        normalize,
    ])
    val_transform = transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.ToTensor(),
        normalize,
    ])

    if model.lower() == 'inception_v3':  # change input size to 299
        train_transform.transforms[0].size = (299, 299)
        val_transform.transforms[0].size = (299, 299)
    trainset = create_trainset(train_file, label_file, im_path,
                               train_transform, classes)
    valset = create_valset(test_file, im_path, val_transform,
                           trainset.labels_to_ints)
    num_labels = [len(trainset.labels_to_ints[c]) for c in classes]

    # PARAMETERS
    use_cuda = use_gpu and torch.cuda.is_available()
    if use_cuda:
        torch.cuda.set_device(device)
        torch.cuda.manual_seed_all(seed)

    if model.lower() not in [
            'squeezenet', 'mobilenet_v1', 'mobilenet_v2', 'vgg16_bn',
            'inception_v3', 'alexnet'
    ]:
        assert False, 'Unknown model {}\n\t+ Choose from: ' \
                      '[sqeezenet, mobilenet_v1, mobilenet_v2, vgg16_bn, inception_v3, alexnet].'.format(model)
    elif model.lower() == 'mobilenet_v1':
        bodynet = mobilenet_v1(pretrained=weight_file is None)
    elif model.lower() == 'mobilenet_v2':
        bodynet = mobilenet_v2(pretrained=weight_file is None)
    elif model.lower() == 'vgg16_bn':
        bodynet = vgg16_bn(pretrained=weight_file is None)
    elif model.lower() == 'inception_v3':
        bodynet = inception_v3(pretrained=weight_file is None)
    elif model.lower() == 'alexnet':
        bodynet = alexnet(pretrained=weight_file is None)
    else:  # squeezenet
        bodynet = squeezenet(pretrained=weight_file is None)

    # Load weights for the body network
    if weight_file is not None:
        print("=> loading weights from '{}'".format(weight_file))
        pretrained_dict = torch.load(
            weight_file,
            map_location=lambda storage, loc: storage)['state_dict']
        state_dict = bodynet.state_dict()
        pretrained_dict = {
            k.replace('bodynet.', ''): v
            for k, v in
            pretrained_dict.items()  # in case of multilabel weight file
            if (k.replace('bodynet.', '') in state_dict.keys()
                and v.shape == state_dict[k.replace('bodynet.', '')].shape)
        }  # number of classes might have changed
        # check which weights will be transferred
        if not pretrained_dict == state_dict:  # some changes were made
            for k in set(state_dict.keys() + pretrained_dict.keys()):
                if k in state_dict.keys() and k not in pretrained_dict.keys():
                    print('\tWeights for "{}" were not found in weight file.'.
                          format(k))
                elif k in pretrained_dict.keys() and k not in state_dict.keys(
                ):
                    print(
                        '\tWeights for "{}" were are not part of the used model.'
                        .format(k))
                elif state_dict[k].shape != pretrained_dict[k].shape:
                    print(
                        '\tShapes of "{}" are different in model ({}) and weight file ({}).'
                        .format(k, state_dict[k].shape,
                                pretrained_dict[k].shape))
                else:  # everything is good
                    pass

        state_dict.update(pretrained_dict)
        bodynet.load_state_dict(state_dict)

    net = OctopusNet(bodynet, n_labels=num_labels)

    n_parameters = sum(
        [p.data.nelement() for p in net.parameters() if p.requires_grad])
    if use_cuda:
        net = net.cuda()
    print('Using {}\n\t+ Number of params: {}'.format(
        str(net).split('(', 1)[0], n_parameters))

    if not os.path.isdir(log_dir):
        os.makedirs(log_dir)

    # tensorboard summary writer
    timestamp = time.strftime('%m-%d-%H-%M')
    expname = timestamp + '_' + str(net).split('(', 1)[0]
    if exp_name is not None:
        expname = expname + '_' + exp_name
    log = TBPlotter(os.path.join(log_dir, 'tensorboard', expname))
    log.print_logdir()

    # allow auto-tuner to find best algorithm for the hardware
    cudnn.benchmark = True

    with open(label_file, 'rb') as f:
        labels = pickle.load(f)['labels']
        n_labeled = '\t'.join(
            [str(Counter(l).items()) for l in labels.transpose()])

    write_config(argvars,
                 os.path.join(log_dir, expname),
                 extras={'n_labeled': n_labeled})

    # ININTIALIZE TRAINING
    optimizer = optim.SGD(net.parameters(), lr=lr, momentum=momentum)
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,
                                                     'min',
                                                     patience=10,
                                                     threshold=1e-1,
                                                     verbose=True)
    criterion = nn.CrossEntropyLoss()
    if use_cuda:
        criterion = criterion.cuda()

    kwargs = {'num_workers': 4} if use_cuda else {}
    trainloader = DataLoader(trainset,
                             batch_size=batch_size,
                             shuffle=True,
                             **kwargs)
    valloader = DataLoader(valset,
                           batch_size=batch_size,
                           shuffle=True,
                           **kwargs)

    # optionally resume from a checkpoint
    start_epoch = 1
    if chkpt is not None:
        if os.path.isfile(chkpt):
            print("=> loading checkpoint '{}'".format(chkpt))
            checkpoint = torch.load(chkpt,
                                    map_location=lambda storage, loc: storage)
            start_epoch = checkpoint['epoch']
            best_acc_score = checkpoint['best_acc_score']
            best_acc = checkpoint['acc']
            net.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                chkpt, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(chkpt))

    def train(epoch):
        losses = AverageMeter()
        accs = AverageMeter()
        class_acc = [AverageMeter() for i in range(len(classes))]

        # switch to train mode
        net.train()
        for batch_idx, (data, target) in enumerate(trainloader):
            if use_cuda:
                data, target = Variable(
                    data.cuda()), [Variable(t.cuda()) for t in target]
            else:
                data, target = Variable(data), [Variable(t) for t in target]

            # compute output
            outputs = net(data)
            preds = [torch.max(outputs[i], 1)[1] for i in range(len(classes))]

            loss = Variable(torch.Tensor([0]),
                            requires_grad=True).type_as(data[0])
            for i, o, t, p in zip(range(len(classes)), outputs, target, preds):
                # filter unlabeled samples if there are any (have label -1)
                labeled = (t != -1).nonzero().view(-1)
                o, t, p = o[labeled], t[labeled], p[labeled]
                loss += criterion(o, t)
                # measure class accuracy and record loss
                class_acc[i].update(
                    (torch.sum(p == t).type(torch.FloatTensor) /
                     t.size(0)).data)
            accs.update(
                torch.mean(
                    torch.stack(
                        [class_acc[i].val for i in range(len(classes))])),
                target[0].size(0))
            losses.update(loss.data, target[0].size(0))

            # compute gradient and do optimizer step
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if batch_idx % log_interval == 0:
                print('Train Epoch: {} [{}/{}]\t'
                      'Loss: {:.4f} ({:.4f})\t'
                      'Acc: {:.2f}% ({:.2f}%)'.format(epoch,
                                                      batch_idx * len(target),
                                                      len(trainloader.dataset),
                                                      float(losses.val),
                                                      float(losses.avg),
                                                      float(accs.val) * 100.,
                                                      float(accs.avg) * 100.))
                print('\t' + '\n\t'.join([
                    '{}: {:.2f}%'.format(classes[i],
                                         float(class_acc[i].val) * 100.)
                    for i in range(len(classes))
                ]))

        # log avg values to somewhere
        log.write('loss', float(losses.avg), epoch, test=False)
        log.write('acc', float(accs.avg), epoch, test=False)
        for i in range(len(classes)):
            log.write('class_acc', float(class_acc[i].avg), epoch, test=False)

    def test(epoch):
        losses = AverageMeter()
        accs = AverageMeter()
        class_acc = [AverageMeter() for i in range(len(classes))]

        # switch to evaluation mode
        net.eval()
        for batch_idx, (data, target) in enumerate(valloader):
            if use_cuda:
                data, target = Variable(
                    data.cuda()), [Variable(t.cuda()) for t in target]
            else:
                data, target = Variable(data), [Variable(t) for t in target]

            # compute output
            outputs = net(data)
            preds = [torch.max(outputs[i], 1)[1] for i in range(len(classes))]

            loss = Variable(torch.Tensor([0]),
                            requires_grad=True).type_as(data[0])
            for i, o, t, p in zip(range(len(classes)), outputs, target, preds):
                labeled = (t != -1).nonzero().view(-1)
                loss += criterion(o[labeled], t[labeled])
                # measure class accuracy and record loss
                class_acc[i].update((torch.sum(p[labeled] == t[labeled]).type(
                    torch.FloatTensor) / t[labeled].size(0)).data)
            accs.update(
                torch.mean(
                    torch.stack(
                        [class_acc[i].val for i in range(len(classes))])),
                target[0].size(0))
            losses.update(loss.data, target[0].size(0))

        score = accs.avg - torch.std(
            torch.stack([class_acc[i].avg for i in range(len(classes))])
        ) / accs.avg  # compute mean - std/mean as measure for accuracy
        print(
            '\nVal set: Average loss: {:.4f} Average acc {:.2f}% Acc score {:.2f} LR: {:.6f}'
            .format(float(losses.avg),
                    float(accs.avg) * 100., float(score),
                    optimizer.param_groups[-1]['lr']))
        print('\t' + '\n\t'.join([
            '{}: {:.2f}%'.format(classes[i],
                                 float(class_acc[i].avg) * 100.)
            for i in range(len(classes))
        ]))
        log.write('loss', float(losses.avg), epoch, test=True)
        log.write('acc', float(accs.avg), epoch, test=True)
        for i in range(len(classes)):
            log.write('class_acc', float(class_acc[i].avg), epoch, test=True)
        return losses.avg.cpu().numpy(), float(score), float(
            accs.avg), [float(class_acc[i].avg) for i in range(len(classes))]

    if start_epoch == 1:  # compute baseline:
        _, best_acc_score, best_acc, _ = test(epoch=0)
    else:  # checkpoint was loaded
        best_acc_score = best_acc_score
        best_acc = best_acc

    for epoch in range(start_epoch, epochs + 1):
        # train for one epoch
        train(epoch)
        # evaluate on validation set
        val_loss, val_acc_score, val_acc, val_class_accs = test(epoch)
        scheduler.step(val_loss)

        # remember best acc and save checkpoint
        is_best = val_acc_score > best_acc_score
        best_acc_score = max(val_acc_score, best_acc_score)
        save_checkpoint(
            {
                'epoch': epoch,
                'state_dict': net.state_dict(),
                'best_acc_score': best_acc_score,
                'acc': val_acc,
                'class_acc': {c: a
                              for c, a in zip(classes, val_class_accs)}
            },
            is_best,
            expname,
            directory=log_dir)

        if val_acc > best_acc:
            shutil.copyfile(
                os.path.join(log_dir, expname + '_checkpoint.pth.tar'),
                os.path.join(log_dir,
                             expname + '_model_best_mean_acc.pth.tar'))
        best_acc = max(val_acc, best_acc)

        if optimizer.param_groups[-1]['lr'] < 1e-5:
            print('Learning rate reached minimum threshold. End training.')
            break

    # report best values
    best = torch.load(os.path.join(log_dir, expname + '_model_best.pth.tar'),
                      map_location=lambda storage, loc: storage)
    print(
        'Finished training after epoch {}:\n\tbest acc score: {}\n\tacc: {}\n\t class acc: {}'
        .format(best['epoch'], best['best_acc_score'], best['acc'],
                best['class_acc']))
    print('Best model mean accuracy: {}'.format(best_acc))
예제 #16
0
                     type=str,
                     default=os.path.join(os.path.dirname(__file__),
                                          '../tools/bvlc_alexnet.prototxt'),
                     help='path to the Caffe prototxt')
 parser.add_argument('--model',
                     type=str,
                     default=os.path.join(
                         os.path.dirname(__file__),
                         '../tools/bvlc_alexnet.caffemodel'),
                     help='path to the Caffe model')
 args = parser.parse_args()
 images = tf.placeholder(tf.float32,
                         shape=[None, 227, 227, 3],
                         name='images')
 train = tf.placeholder_with_default(True, [], name='train')
 output = models.alexnet(images, train, pretrained=True)
 sess = tf.Session()
 init = tf.global_variables_initializer()
 data = np.random.rand(10, 227, 227, 3) * 4 + 150
 sess.run(init)
 result = sess.run(output, feed_dict={images: data, train: False})
 caffe_data = data.transpose([0, 3, 1, 2])[:, ::-1]
 net = caffe.Net(args.prototxt, args.model, caffe.TEST)
 net.blobs['data'].reshape(*caffe_data.shape)
 net.blobs['data'].data[:] = caffe_data
 net.forward()
 diff = np.sum((result - net.blobs['fc8'].data)**2)
 if diff < 1e-5:
     print('Test Okay')
 else:
     print('Test failed')
예제 #17
0
    dataset = ImageFolderWithPaths(offset, transform=transforms.Compose(tra))
    dataloader = torch.utils.data.DataLoader(dataset,
                                            batch_size=1,
                                            num_workers=0,
                                            pin_memory=True,
                                            shuffle = False)
    features = compute_features(dataloader, model, len(dataset))
    return features


if __name__ == '__main__':
#    global args
#    args = parser.parse_args()
    modelpth = '/home/CUSACKLAB/annatruzzi/alexnet_models/'
    checkpoint = torch.load(modelpth+'modified_alexnet_checkpoint_27.pth.tar')['state_dict']
    checkpoint_new = OrderedDict()
    for k, v in checkpoint.items():
        name_temp = k.replace(".module", '') # remove '.module' of dataparallel
        name = name_temp.replace("module.",'') # remove 'module.'
        checkpoint_new[name]=v

    model = models.alexnet(sobel=True, bn=True, out=1000) 
    model.load_state_dict(checkpoint_new)
    model.cuda()
    image_pth = '/home/CUSACKLAB/annatruzzi/cichy2016/algonautsChallenge2019/Training_Data/92_Image_Set/92images' 
    act = get_activations(image_pth)

    with open('/home/CUSACKLAB/annatruzzi/cichy2016/niko92_activations_pretrained_alexnet.pickle', 'wb') as handle:
        pickle.dump(act, handle)

예제 #18
0
        args)

    if args.model == 'fc':
        if args.dataset == 'mnist':
            net = fc(width=args.width,
                     depth=args.depth,
                     num_classes=num_classes).to(args.device)
        elif args.dataset == 'cifar10':
            net = fc(width=args.width,
                     depth=args.depth,
                     num_classes=num_classes,
                     input_dim=3 * 32 * 32).to(args.device)
    elif args.model == 'alexnet':
        if args.dataset == 'mnist':
            net = alexnet(input_height=28,
                          input_width=28,
                          input_channels=1,
                          num_classes=num_classes)
        else:
            net = alexnet(ch=args.scale,
                          num_classes=num_classes).to(args.device)
    elif args.model == 'vgg':
        net = vgg(depth=args.depth,
                  num_classes=num_classes,
                  batch_norm=args.bn).to(args.device)

    print(net)

    opt = getattr(optim, args.optim)(net.parameters(), lr=args.lr)

    if args.lr_schedule:
        milestone = int(args.iterations / 3)
예제 #19
0
    args.use_cuda = not args.no_cuda and torch.cuda.is_available()
    args.device = torch.device('cuda' if args.use_cuda else 'cpu')
    torch.manual_seed(args.seed)
    
    print(args)

    # training setup
    train_loader, test_loader_eval, train_loader_eval, num_classes = get_data(args)

    if args.model == 'fc':
        if args.dataset == 'mnist':
            net = fc(width=args.width, depth=args.depth, num_classes=num_classes).to(args.device)
        elif args.dataset == 'cifar10':
            net = fc(width=args.width, depth=args.depth, num_classes=num_classes, input_dim=3*32*32).to(args.device)
    elif args.model == 'alexnet':
        net = alexnet(ch=args.scale, num_classes=num_classes).to(args.device)
    elif args.model == 'simplenet':
        net = simplenet().to(args.device)

    print(net)
    
    opt = optim.SGD(
        net.parameters(), 
        lr=args.lr, 
        momentum=args.mom,
        weight_decay=args.wd
        )

    if args.lr_schedule:
        milestone = int(args.iterations / 3)
        scheduler = optim.lr_scheduler.MultiStepLR(opt, 
예제 #20
0
                else:
                    net = fc(width=args.width,
                             depth=args.depth,
                             num_classes=num_classes,
                             activation=args.activation,
                             use_batch_norm=args.batch_norm).to(args.device)
            elif args.dataset == 'cifar10':
                net = fc(width=args.width,
                         depth=args.depth,
                         num_classes=num_classes,
                         input_dim=3 * 32 * 32,
                         activation=args.activation,
                         use_batch_norm=args.batch_norm).to(args.device)
        elif args.model == 'alexnet':
            net = alexnet(ch=args.scale,
                          num_classes=num_classes,
                          use_batch_norm=args.batch_norm).to(args.device)
        elif args.model == 'lenet':
            net = lenet().to(args.device)
        elif 'VGG' in args.model:
            net = VGG(args.model, batch_norm=args.batch_norm).to(args.device)
        elif 'Resnet' in args.model:
            net = ResNet18(use_batch_norm=args.batch_norm).to(args.device)

        training_history = []
        weight_grad_history = []

        # eval logs less frequently
        evaluation_history_TEST = []
        evaluation_history_TRAIN = []
        noise_norm_history_TEST = []
예제 #21
0
sys.path.insert(0, '../../python')

import planner as pln
import hardware as hw
import dataset
import models

import torch.nn
import torch

import time

simd_cfg_path = '../../hwcfg/simd.json'
hw_spec = hw.HardwareSpec(simd_cfg_path)

data = torch.Tensor(1, 3, 227, 227);
alexnet = models.alexnet()

pnn = pln.Planner()

start_time = time.time()

for name, module in alexnet.named_modules():
    if isinstance(module, torch.nn.Sequential):
        continue
    pnn.set_data(data=data, module=module, hw_spec=hw_spec, layer_name=name)
    data = pnn.run('../../build')

elapsed_time = time.time() - start_time
print('[Front-end] Elapsed time: ' + time.strftime('%H hours %M min %S sec', time.gmtime(elapsed_time)))
예제 #22
0
def main():
	model = alexnet(WIDTH, HEIGHT, LR)
	#model.load('trained_models/' + MODEL_NAME % ('alexnet', EPOCHS, DATA_TYPE))
	model.load('model/model.tflearn')    
	timer(5)

	# 800x600 windowed mode
	mon = {"top": 60, "left": 0, "width": 800, "height": 600}

	# Screenshot object
	sct = mss.mss()

	paused = False
	count_dk = 0
	count_st = 0
	
	while True:

		if not paused:
			image = np.asarray(sct.grab(mon))
			# Converting to grayscale, reason much smaller then RGB(minus 2 D)
			image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
			# Resizing image to 80x60waw
			image = cv2.resize(image, (160, 120))

			prediction = model.predict([image.reshape(160, 120, 1)])[0]
			#print(prediction)
			#print(np.argmax(prediction))

		
			turn_thresh = .7
			fwd_thresh = 0.7

			if prediction[1] > fwd_thresh:
				count_dk = 0
				count_st += 1
				if count_st > 1:
					print('STOP ST')
					stop()
					count_st = 0

				print('STRAIGHT : %s' % (prediction[1]*100.0))
				straight()
			elif prediction[0] > turn_thresh:
				count_dk = 0
				count_st = 0
				
				print('LEFT : %s' % (prediction[0]*100.0))
				left()
			elif prediction[2] > turn_thresh:
				count_dk = 0
				count_st = 0
				
				print('RIGHT : %s' % (prediction[2]*100.0))
				right()
			else:
				count_dk += 1
				count_st = 0
				
				if count_dk > 3:
					print('STOP DK')
					stop()
					count_dk = 0

				print('DK : STRAIGHT')
				
				dk_straight()
			

			
		# p pauses game and can get annoying.
		if keys.output == 'pause':
			if paused:
				paused = False
			else:
				paused = True
				stop()
				#time.sleep(0.2)
		elif keys.output == 'quit':
			return True
예제 #23
0
def train_multiclass(train_file, test_file, stat_file,
                     model='mobilenet_v2',
                     classes=('artist_name', 'genre', 'style', 'technique', 'century'),
                     label_file='_user_labels.pkl',
                     im_path='/export/home/kschwarz/Documents/Data/Wikiart_artist49_images',
                     chkpt=None, weight_file=None,
                     triplet_selector='semihard', margin=0.2,
                     labels_per_class=4, samples_per_label=4,
                     use_gpu=True, device=0,
                     epochs=100, batch_size=32, lr=1e-4, momentum=0.9,
                     log_interval=10, log_dir='runs',
                     exp_name=None, seed=123):
    argvars = locals().copy()
    torch.manual_seed(seed)

    # LOAD DATASET
    with open(stat_file, 'r') as f:
        data = pickle.load(f)
        mean, std = data['mean'], data['std']
        mean = [float(m) for m in mean]
        std = [float(s) for s in std]
    normalize = transforms.Normalize(mean=mean, std=std)
    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(90),
        transforms.ToTensor(),
        normalize,
    ])
    val_transform = transforms.Compose([
                transforms.RandomResizedCrop(224),
                transforms.ToTensor(),
                normalize,
    ])

    if model.lower() == 'inception_v3':            # change input size to 299
        train_transform.transforms[0].size = (299, 299)
        val_transform.transforms[0].size = (299, 299)
    trainset = create_trainset(train_file, label_file, im_path, train_transform, classes)
    for c in classes:
        if len(trainset.labels_to_ints[c]) < labels_per_class:
            print('less labels in class {} than labels_per_class, use all available labels ({})'
                  .format(c, len(trainset.labels_to_ints[c])))
    valset = create_valset(test_file, im_path, val_transform, trainset.labels_to_ints)
    # PARAMETERS
    use_cuda = use_gpu and torch.cuda.is_available()
    if use_cuda:
        torch.cuda.set_device(device)
        torch.cuda.manual_seed_all(seed)

    if model.lower() not in ['squeezenet', 'mobilenet_v1', 'mobilenet_v2', 'vgg16_bn', 'inception_v3', 'alexnet']:
        assert False, 'Unknown model {}\n\t+ Choose from: ' \
                      '[sqeezenet, mobilenet_v1, mobilenet_v2, vgg16_bn, inception_v3, alexnet].'.format(model)
    elif model.lower() == 'mobilenet_v1':
        bodynet = mobilenet_v1(pretrained=weight_file is None)
    elif model.lower() == 'mobilenet_v2':
        bodynet = mobilenet_v2(pretrained=weight_file is None)
    elif model.lower() == 'vgg16_bn':
        bodynet = vgg16_bn(pretrained=weight_file is None)
    elif model.lower() == 'inception_v3':
        bodynet = inception_v3(pretrained=weight_file is None)
    elif model.lower() == 'alexnet':
        bodynet = alexnet(pretrained=weight_file is None)
    else:       # squeezenet
        bodynet = squeezenet(pretrained=weight_file is None)

    # Load weights for the body network
    if weight_file is not None:
        print("=> loading weights from '{}'".format(weight_file))
        pretrained_dict = torch.load(weight_file, map_location=lambda storage, loc: storage)['state_dict']
        state_dict = bodynet.state_dict()
        pretrained_dict = {k.replace('bodynet.', ''): v for k, v in pretrained_dict.items()         # in case of multilabel weight file
                           if (k.replace('bodynet.', '') in state_dict.keys() and v.shape == state_dict[k.replace('bodynet.', '')].shape)}  # number of classes might have changed
        # check which weights will be transferred
        if not pretrained_dict == state_dict:  # some changes were made
            for k in set(state_dict.keys() + pretrained_dict.keys()):
                if k in state_dict.keys() and k not in pretrained_dict.keys():
                    print('\tWeights for "{}" were not found in weight file.'.format(k))
                elif k in pretrained_dict.keys() and k not in state_dict.keys():
                    print('\tWeights for "{}" were are not part of the used model.'.format(k))
                elif state_dict[k].shape != pretrained_dict[k].shape:
                    print('\tShapes of "{}" are different in model ({}) and weight file ({}).'.
                          format(k, state_dict[k].shape, pretrained_dict[k].shape))
                else:  # everything is good
                    pass

        state_dict.update(pretrained_dict)
        bodynet.load_state_dict(state_dict)

    net = MetricNet(bodynet, len(classes))

    n_parameters = sum([p.data.nelement() for p in net.parameters() if p.requires_grad])
    if use_cuda:
        net = net.cuda()
    print('Using {}\n\t+ Number of params: {}'.format(str(net).split('(', 1)[0], n_parameters))

    if not os.path.isdir(log_dir):
        os.makedirs(log_dir)

    # tensorboard summary writer
    timestamp = time.strftime('%m-%d-%H-%M')
    expname = timestamp + '_' + str(net).split('(', 1)[0]
    if exp_name is not None:
        expname = expname + '_' + exp_name
    log = TBPlotter(os.path.join(log_dir, 'tensorboard', expname))
    log.print_logdir()

    # allow auto-tuner to find best algorithm for the hardware
    cudnn.benchmark = True

    with open(label_file, 'rb') as f:
        labels = pickle.load(f)['labels']
        n_labeled = '\t'.join([str(Counter(l).items()) for l in labels.transpose()])

    write_config(argvars, os.path.join(log_dir, expname), extras={'n_labeled': n_labeled})


    # ININTIALIZE TRAINING
    optimizer = optim.SGD(net.parameters(), lr=lr, momentum=momentum)
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=10, threshold=1e-1, verbose=True)

    if triplet_selector.lower() not in ['random', 'semihard', 'hardest', 'mixed', 'khardest']:
        assert False, 'Unknown option {} for triplet selector. Choose from "random", "semihard", "hardest" or "mixed"' \
                      '.'.format(triplet_selector)
    elif triplet_selector.lower() == 'random':
        criterion = TripletLoss(margin=margin,
                                triplet_selector=RandomNegativeTripletSelector(margin, cpu=not use_cuda))
    elif triplet_selector.lower() == 'semihard' or triplet_selector.lower() == 'mixed':
        criterion = TripletLoss(margin=margin,
                                triplet_selector=SemihardNegativeTripletSelector(margin, cpu=not use_cuda))
    elif triplet_selector.lower() == 'khardest':
        criterion = TripletLoss(margin=margin,
                                triplet_selector=KHardestNegativeTripletSelector(margin, k=3, cpu=not use_cuda))
    else:
        criterion = TripletLoss(margin=margin,
                                triplet_selector=HardestNegativeTripletSelector(margin, cpu=not use_cuda))
    if use_cuda:
        criterion = criterion.cuda()

    kwargs = {'num_workers': 4} if use_cuda else {}
    multilabel_train = np.stack([trainset.df[c].values for c in classes]).transpose()
    train_batch_sampler = BalancedBatchSamplerMulticlass(multilabel_train, n_label=labels_per_class,
                                                         n_per_label=samples_per_label, ignore_label=None)
    trainloader = DataLoader(trainset, batch_sampler=train_batch_sampler, **kwargs)
    multilabel_val = np.stack([valset.df[c].values for c in classes]).transpose()
    val_batch_sampler = BalancedBatchSamplerMulticlass(multilabel_val, n_label=labels_per_class,
                                                       n_per_label=samples_per_label, ignore_label=None)
    valloader = DataLoader(valset, batch_sampler=val_batch_sampler, **kwargs)

    # optionally resume from a checkpoint
    start_epoch = 1
    if chkpt is not None:
        if os.path.isfile(chkpt):
            print("=> loading checkpoint '{}'".format(chkpt))
            checkpoint = torch.load(chkpt, map_location=lambda storage, loc: storage)
            start_epoch = checkpoint['epoch']
            best_acc_score = checkpoint['best_acc_score']
            best_acc = checkpoint['acc']
            net.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(chkpt, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(chkpt))

    def train(epoch):
        losses = AverageMeter()
        gtes = AverageMeter()
        non_zero_triplets = AverageMeter()
        distances_ap = AverageMeter()
        distances_an = AverageMeter()

        # switch to train mode
        net.train()
        for batch_idx, (data, target) in enumerate(trainloader):
            target = torch.stack(target)
            if use_cuda:
                data, target = Variable(data.cuda()), [Variable(t.cuda()) for t in target]
            else:
                data, target = Variable(data), [Variable(t) for t in target]

            # compute output
            outputs = net(data)

            # normalize features
            for i in range(len(classes)):
                outputs[i] = torch.nn.functional.normalize(outputs[i], p=2, dim=1)

            loss = Variable(torch.Tensor([0]), requires_grad=True).type_as(data[0])
            n_triplets = 0
            for op, tgt in zip(outputs, target):
                # filter unlabeled samples if there are any (have label -1)
                labeled = (tgt != -1).nonzero().view(-1)
                op, tgt = op[labeled], tgt[labeled]

                l, nt = criterion(op, tgt)
                loss += l
                n_triplets += nt

            non_zero_triplets.update(n_triplets, target[0].size(0))
            # measure GTE and record loss
            gte, dist_ap, dist_an = GTEMulticlass(outputs, target)           # do not compute ap pairs for concealed classes
            gtes.update(gte.data, target[0].size(0))
            distances_ap.update(dist_ap.data, target[0].size(0))
            distances_an.update(dist_an.data, target[0].size(0))
            losses.update(loss.data[0], target[0].size(0))

            # compute gradient and do optimizer step
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if batch_idx % log_interval == 0:
                print('Train Epoch: {} [{}/{}]\t'
                      'Loss: {:.4f} ({:.4f})\t'
                      'GTE: {:.2f}% ({:.2f}%)\t'
                      'Non-zero Triplets: {:d} ({:d})'.format(
                    epoch, batch_idx * len(target[0]), len(trainloader) * len(target[0]),
                    float(losses.val), float(losses.avg),
                    float(gtes.val) * 100., float(gtes.avg) * 100.,
                    int(non_zero_triplets.val), int(non_zero_triplets.avg)))

        # log avg values to somewhere
        log.write('loss', float(losses.avg), epoch, test=False)
        log.write('gte', float(gtes.avg), epoch, test=False)
        log.write('non-zero trplts', int(non_zero_triplets.avg), epoch, test=False)
        log.write('dist_ap', float(distances_ap.avg), epoch, test=False)
        log.write('dist_an', float(distances_an.avg), epoch, test=False)

    def test(epoch):
        losses = AverageMeter()
        gtes = AverageMeter()
        non_zero_triplets = AverageMeter()
        distances_ap = AverageMeter()
        distances_an = AverageMeter()

        # switch to evaluation mode
        net.eval()
        for batch_idx, (data, target) in enumerate(valloader):
            target = torch.stack(target)
            if use_cuda:
                data, target = Variable(data.cuda()), [Variable(t.cuda()) for t in target]
            else:
                data, target = Variable(data), [Variable(t) for t in target]
            # compute output
            outputs = net(data)

            # normalize features
            for i in range(len(classes)):
                outputs[i] = torch.nn.functional.normalize(outputs[i], p=2, dim=1)

            loss = Variable(torch.Tensor([0]), requires_grad=True).type_as(data[0])
            n_triplets = 0
            for op, tgt in zip(outputs, target):
                # filter unlabeled samples if there are any (have label -1)
                labeled = (tgt != -1).nonzero().view(-1)
                op, tgt = op[labeled], tgt[labeled]

                l, nt = criterion(op, tgt)
                loss += l
                n_triplets += nt

            non_zero_triplets.update(n_triplets, target[0].size(0))
            # measure GTE and record loss
            gte, dist_ap, dist_an = GTEMulticlass(outputs, target)
            gtes.update(gte.data.cpu(), target[0].size(0))
            distances_ap.update(dist_ap.data.cpu(), target[0].size(0))
            distances_an.update(dist_an.data.cpu(), target[0].size(0))
            losses.update(loss.data[0].cpu(), target[0].size(0))

        print('\nVal set: Average loss: {:.4f} Average GTE {:.2f}%, '
              'Average non-zero triplets: {:d} LR: {:.6f}'.format(float(losses.avg), float(gtes.avg) * 100.,
                                                       int(non_zero_triplets.avg),
                                                                  optimizer.param_groups[-1]['lr']))
        log.write('loss', float(losses.avg), epoch, test=True)
        log.write('gte', float(gtes.avg), epoch, test=True)
        log.write('non-zero trplts', int(non_zero_triplets.avg), epoch, test=True)
        log.write('dist_ap', float(distances_ap.avg), epoch, test=True)
        log.write('dist_an', float(distances_an.avg), epoch, test=True)
        return losses.avg, 1 - gtes.avg

    if start_epoch == 1:         # compute baseline:
        _, best_acc = test(epoch=0)
    else:       # checkpoint was loaded
        best_acc = best_acc

    for epoch in range(start_epoch, epochs + 1):
        if triplet_selector.lower() == 'mixed' and epoch == 26:
            criterion.triplet_selector = HardestNegativeTripletSelector(margin, cpu=not use_cuda)
            print('Changed negative selection from semihard to hardest.')
        # train for one epoch
        train(epoch)
        # evaluate on validation set
        val_loss, val_acc = test(epoch)
        scheduler.step(val_loss)

        # remember best acc and save checkpoint
        is_best = val_acc > best_acc
        best_acc = max(val_acc, best_acc)
        save_checkpoint({
            'epoch': epoch,
            'state_dict': net.state_dict(),
            'best_acc': best_acc,
        }, is_best, expname, directory=log_dir)

        if optimizer.param_groups[-1]['lr'] < 1e-5:
            print('Learning rate reached minimum threshold. End training.')
            break

    # report best values
    best = torch.load(os.path.join(log_dir, expname + '_model_best.pth.tar'), map_location=lambda storage, loc: storage)
    print('Finished training after epoch {}:\n\tbest acc score: {}'
          .format(best['epoch'], best['acc']))
    print('Best model mean accuracy: {}'.format(best_acc))
예제 #24
0
def main():
    epoch = 2
    class_number = 2
    chosen_model = "resnet50"
    transform = googlenet_transform()

    if chosen_model == "alexnet":
        model = alexnet(class_num = class_number)
    elif chosen_model == "googlenet":
        model = googlenet(class_num = class_number)
    elif chosen_model == "resnet18":
        model = resnet18(class_num = class_number)
    elif chosen_model == "resnet34":
        model = resnet34(class_num = class_number)
    elif chosen_model == "resnet50":
        model = resnet50(class_num = class_number)
    elif chosen_model == "resnet101":
        model = resnet101(class_num = class_number)
    elif chosen_model == "resnet152":
        model = resnet152(class_num = class_number)


    use_gpu = torch.cuda.is_available()
    if use_gpu:
        model = model.cuda()
        print("Use GPU")
    else:
        print("Use CPU")
    train_txt_path = "/Users/Barry/Desktop/classification_models_pytorch/trainset.txt"
    trainset = Mydataset(train_txt_path, transform)
    trainloader = DataLoader(trainset, 
                             batch_size = 3, 
                             shuffle=True
                             )
    val_txt_path = "/Users/Barry/Desktop/classification_models_pytorch/valset.txt"
    valset = Mydataset(val_txt_path, transform)
    valloader = DataLoader(valset, 
                             batch_size = 3, 
                             shuffle=True
                             )
    
    print("data loaded")
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr = 0.001 )

    print("start to train")
    for i in range(epoch):
        model.train()
        for batch_index, (data, target) in enumerate(trainloader):
            if use_gpu:
                data = data.cuda()
                target = target.cuda()
            data, label = Variable(data), Variable(target)
            output = model(data)
            loss = criterion(output, target)
            loss.backward()
            optimizer.step()
            
            if batch_index > 0 and batch_index % 50 == 0:
                print("Trian epoch {},  iterate {}, loss is {}".format(i, batch_index, loss))
            # validate prcess
            if batch_index > 0 and batch_index % 500 == 0:
                model.eval()
                losses = []
                for val_index, (data, target) in enumerate(valloader):
                    data, label = Variable(data), Variable(target)
                    output = model(data)
                    loss = criterion(output, target)
                    losses.append(loss.data)
                    if val_index > 0 and val_index % 100 == 0:
                        break

                result = np.mean(losses)
                print("validation loss is {}".format(result))
                model.train()
예제 #25
0
def build_models(model_name):
    """Creates three different models, one used for source only training,
    two used for domain adaptation
    """
    inputs = Input(shape=(224, 224, 3))
    if model_name == "alexnet":
        x4 = alexnet(inputs)

    elif model_name == "vgg16":
        x4 = vgg_16(inputs)

    x4 = Flatten()(x4)

    x4 = Dense(32, activation="relu")(x4)
    x4 = BatchNormalization()(x4)
    x4 = Activation("elu")(x4)

    source_classifier = Dense(2, activation="softmax", name="mo")(x4)

    # Domain Classification
    domain_classifier = Dense(16, activation="relu", name="do4")(x4)
    domain_classifier = BatchNormalization(name="do5")(domain_classifier)
    domain_classifier = Activation("elu", name="do6")(domain_classifier)
    domain_classifier = Dropout(0.5)(domain_classifier)

    domain_classifier = Dense(2, activation="softmax",
                              name="do")(domain_classifier)

    # Combined model
    comb_model = Model(inputs=inputs,
                       outputs=[source_classifier, domain_classifier])

    comb_model.compile(
        optimizer="Adam",
        loss={
            "mo": "categorical_crossentropy",
            "do": "categorical_crossentropy"
        },
        loss_weights={
            "mo": 1,
            "do": 2
        },
        metrics=["accuracy"],
    )

    source_classification_model = Model(
        inputs=inputs,
        outputs=[source_classifier],
    )

    source_classification_model.compile(
        optimizer="Adam",
        loss={"mo": "categorical_crossentropy"},
        metrics=["accuracy"],
    )

    domain_classification_model = Model(inputs=inputs,
                                        outputs=[domain_classifier])

    domain_classification_model.compile(
        optimizer="Adam",
        loss={"do": "categorical_crossentropy"},
        metrics=["accuracy"])

    embeddings_model = Model(inputs=inputs, outputs=[x4])

    embeddings_model.compile(optimizer="Adam",
                             loss="categorical_crossentropy",
                             metrics=["accuracy"])

    return (
        comb_model,
        source_classification_model,
        domain_classification_model,
        embeddings_model,
    )
예제 #26
0
def print_model_parm_nums():
    model = models.alexnet()
    total = sum([param.nelement() for param in model.parameters()])
    print('  + Number of params: %.2fM' % (total / 1e6))
예제 #27
0
parser.add_argument('--seed', type=int, default=1, metavar='S',
                    help='random seed for replicable result (default: 1)')
parser.add_argument('--saves', metavar='NAME',
                    help='the name to saves the model after training (default: None, it dont saves)')
args = parser.parse_args()


args.cuda = not args.no_cuda and torch.cuda.is_available()
if args.dataset not in datasets.available_datasets:
    print("Dataset not available")
    exit()

num_classes = datasets.num_classes[args.dataset]

if args.pretrained:
    cnn = models.alexnet(num_classes)
else:
    cnn = torchvision.models.alexnet(pretrained=False, num_classes=num_classes)

torch.manual_seed(args.seed)
if args.cuda:
    torch.cuda.manual_seed(args.seed)

dataset = datasets.__dict__[args.dataset]
train_loader, test_loader = dataset(batch_size=args.batch_size, download=False)

if args.cuda:
    cnn.cuda()

optimizer = optim.Adam(cnn.parameters(), lr=args.lr)
criterion = nn.CrossEntropyLoss()