Пример #1
0
def make_chainerVGGFace_fc8_layer():
    caffeVGG_as_chainerVGG = '/home/gabi/PycharmProjects/visualizing-traits/src/VGGFace/chainerVGGFace'

    # load model
    model = VGG16Layers(pretrained_model=False)
    model.fc8.out_size = 2622
    model.fc8.b = np.zeros(2622, dtype=np.float32)
    model.fc8.W = np.zeros((2622, 4096), dtype=np.float32)
    chainer.serializers.load_npz(caffeVGG_as_chainerVGG, model)
    print('loading successful')
    labels = list(
        np.genfromtxt('/media/gabi/DATADRIVE1/datasets/VGGFace/names.txt',
                      dtype=str))
    example = '/home/gabi/PycharmProjects/visualizing-traits/src/VGGFace/alan.jpg'
    example_data = ndimage.imread(example).astype(np.float32)
    s = np.shape(example_data)
    example_data = np.reshape(example_data, (s[2], s[0], s[1]))
    example_data = np.expand_dims(example_data, 0)

    # x = Variable(example_data)

    with chainer.using_config('train', False):
        y = model(example_data)
    y = y['prob'].data[0]
    # y = y.data
    i = np.argmax(y)
    print(labels[i])
Пример #2
0
    def __init__(self, n_class=21):
        self.n_class = n_class
        deconv_filter = upsample_filt(64)
        deconv_filter = np.broadcast_to(deconv_filter,
                                        (self.n_class, self.n_class, 64, 64))
        super(self.__class__, self).__init__(
            vgg=VGG16Layers(),
            fc6=L.Convolution2D(512, 4096, 7, stride=1, pad=0),
            fc7=L.Convolution2D(4096, 4096, 1, stride=1, pad=0),
            score_fr=L.Convolution2D(4096, self.n_class, 1, stride=1, pad=0),
            upscore=L.Deconvolution2D(self.n_class,
                                      self.n_class,
                                      64,
                                      stride=32,
                                      pad=0,
                                      initialW=deconv_filter,
                                      nobias=True),
        )
        # copy fully connected layers' weights to equivalent conv layers
        self.fc6.W.data[:] = self.vgg.fc6.W.data.reshape(self.fc6.W.shape)
        self.fc6.b.data[:] = self.vgg.fc6.b.data.reshape(self.fc6.b.shape)
        self.fc7.W.data[:] = self.vgg.fc7.W.data.reshape(self.fc7.W.shape)
        self.fc7.b.data[:] = self.vgg.fc7.b.data.reshape(self.fc7.b.shape)

        self.vgg.conv1_1.pad = 100

        self.train = False
Пример #3
0
    def __init__(
            self, gpu=-1, rpn_in_ch=512, rpn_out_ch=512,
            n_anchors=9, feat_stride=16, anchor_scales=[8, 16, 32],
            num_classes=21, spatial_scale=0.0625, rpn_sigma=1.0, sigma=3.0,
            nms_thresh=0.3, confidence=0.8
    ):
        # names of links are consistent with the original implementation so
        # that learned parameters can be used
        super(FasterRCNN, self).__init__(
            trunk=VGG16Layers(),
            RPN=RPN(rpn_in_ch, rpn_out_ch, n_anchors, feat_stride,
                    anchor_scales, num_classes, rpn_sigma),
            fc6=L.Linear(25088, 4096),
            fc7=L.Linear(4096, 4096),
            cls_score=L.Linear(4096, num_classes),
            bbox_pred=L.Linear(4096, num_classes * 4),
        )
        vgg_remove_lists = ['fc6', 'fc7', 'fc8']
        for name in vgg_remove_lists:
            self.trunk._children.remove(name)
            delattr(self.trunk, name)

        self.proposal_target_layer = ProposalTargetLayer(num_classes)
        self.train = True
        self.gpu = gpu
        self.sigma = sigma

        self.spatial_scale = spatial_scale

        self.nms_thresh = 0.3
        self.confidence = 0.8
Пример #4
0
def main():
    parser = argparse.ArgumentParser(description='evaluate imagenet')
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--count-by', choices=(None, 'layers', 'functions'),
                        default=None)
    parser.add_argument('model',
                        choices=('vgg16', 'googlenet',
                                 'resnet50', 'resnet101', 'resnet152'))
    args = parser.parse_args()

    if args.model == 'vgg16':
        model = VGG16Layers(pretrained_model=None)
    elif args.model == 'googlenet':
        model = GoogLeNet(pretrained_model=None)
    elif args.model == 'resnet50':
        model = ResNet50Layers(pretrained_model=None)
    elif args.model == 'resnet101':
        model = ResNet101Layers(pretrained_model=None)
    elif args.model == 'resnet152':
        model = ResNet152Layers(pretrained_model=None)
    # override batch_normalization, don't override in prodution
    if 'resnet' in args.model:
        monkey.override_bn()

    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()
        model.to_gpu(args.gpu)
    else:
        if chainer.config.use_ideep != "never":
            model.to_intel64()

    if args.count_by is not None and args.gpu > 0:
        warnings.warn("count_by with GPU is not supported", ValueError)

    if args.count_by == 'layers':
        from perf_counter import Counter
        monkey.decorate_link(model, Counter)
    if args.count_by == 'functions':
        from perf_counter import CounterHook
    else:
        from monkey import CounterHook

    image = np.zeros((1, 3, 224, 224), dtype=np.float32)  # dummy image
    with chainer.using_config('train', False):
        with chainer.using_config('enable_backprop', False):
            with CounterHook() as counter:
                model.predict(image, oversample=False)
    for fn, float_ops in counter.call_history:
        print('"{}","{}"'.format(fn, float_ops))
Пример #5
0
import numpy as np
import chainer

VGGFACE_CAFFE_MODEL = '/media/gabi/DATADRIVE1/datasets/VGGFace/VGG_FACE.caffemodel'
VGGFACE_CAFFE_PROTO = '/media/gabi/DATADRIVE1/datasets/VGGFace/VGG_FACE_deploy.prototxt'
CAFFE_PATH = '/home/gabi/Documents/caffe/python'

sys.path.append(CAFFE_PATH)
import caffe

caffe_model = caffe.Net(VGGFACE_CAFFE_PROTO, VGGFACE_CAFFE_MODEL, caffe.TEST)

fc8_w = caffe_model.params['fc8'][0].data
fc8_b = caffe_model.params['fc8'][1].data

model = VGG16Layers(pretrained_model=False)
model.fc8.out_size = 2622
model.fc8.b = fc8_b
model.fc8.W = fc8_w

caffeVGG_as_chainerVGG = '/home/gabi/PycharmProjects/visualizing-traits/src/VGGFace/chainerVGGFace'
chainer.serializers.load_npz(caffeVGG_as_chainerVGG, model)

# new_fc8_b = model.fc8.b
# new_fc8_w = model.fc8.W
#
# print('b', fc8_b == new_fc8_b)
# print('w', fc8_w == new_fc8_w)

labels = list(
    np.genfromtxt(
Пример #6
0
def download_model(save_name="./VGG16.model"):
    from chainer.links.model.vision.vgg import VGG16Layers
    chainer.serializers.save_npz(save_name, VGG16Layers())
Пример #7
0
from PIL import Image
import time
import h5py as h5
from chainer.links.model.vision.vgg import VGG16Layers
import chainer

t = time.time()

ON_GPU = True
# CELEBA_JPGS = '/home/gabi/Documents/tight_crop_everything/celeba'
# VGG16_CONV3_3_FEATURES_H5 = '/home/gabi/Documents/tight_crop_everything/VGG16_relu3_3_features.h5'
CELEBA_JPGS = '/scratch2/gabi/VGGFACE/data/celeba_tight'
VGG16_CONV3_3_FEATURES_H5 = '/scratch2/gabi/generator/VGG16_relu3_3_features.h5'

if ON_GPU:
    vgg16 = VGG16Layers().to_gpu(device='0')
else:
    vgg16 = VGG16Layers()

all_celeba = os.listdir(CELEBA_JPGS)

action = 'a' if os.path.exists(VGG16_CONV3_3_FEATURES_H5) else 'w'
with h5.File(VGG16_CONV3_3_FEATURES_H5, action) as my_file:
    for i in range(len(all_celeba)):
        print('progress', i, len(all_celeba))
        p = os.path.join(CELEBA_JPGS, all_celeba[i])
        im = ndimage.imread(p).astype(np.float32)
        im[:, :, 0] -= 123.68
        im[:, :, 1] -= 116.779
        im[:, :, 2] -= 103.939
        im = np.asarray(Image.fromarray(im, mode='RGB').resize((224, 224), Image.ANTIALIAS), dtype=np.float32)
Пример #8
0
def training():
    print('setting up...')

    if pc.TRAIN:
        num_features = util.get_number_of_features(pp.CELEB_FACES_FC6_TRAIN)
        # num_features = util.get_number_of_features_from_train(pp.CELEB_FACES_FC6_TRAIN)  # for server
        all_names = np.array(util.get_names_h5_file(pp.FC6_TRAIN_H5))
        path_images = pp.CELEB_FACES_FC6_TRAIN
    else:
        num_features = util.get_number_of_features(pp.CELEB_FACES_FC6_TEST)
        all_names = np.array(util.get_names_h5_file(pp.FC6_TEST_H5))
        path_images = pp.CELEB_FACES_FC6_TEST

    total_steps = num_features / pc.BATCH_SIZE
    mask_L_sti = util.get_L_sti_mask()

    # ----------------------------------------------------------------
    # GENERATOR
    generator = Generator()
    # generator = GeneratorPaper()
    generator_train_loss = np.zeros(pc.EPOCHS)
    generator_optimizer = chainer.optimizers.Adam(alpha=0.0002,
                                                  beta1=0.9,
                                                  beta2=0.999,
                                                  eps=10**-8)
    generator_optimizer.setup(generator)
    # ----------------------------------------------------------------
    # DISCRIMINATOR
    discriminator = Discriminator()
    # discriminator = DiscriminatorPaper()
    discriminator_train_loss = np.zeros(pc.EPOCHS)
    discriminator_optimizer = chainer.optimizers.Adam(alpha=0.0002,
                                                      beta1=0.9,
                                                      beta2=0.999,
                                                      eps=10**-8)
    discriminator_optimizer.setup(discriminator)
    # ----------------------------------------------------------------
    # VGG16 FOR FEATURE LOSS
    vgg16 = VGG16Layers()
    # ----------------------------------------------------------------

    save_list = random.sample(xrange(num_features), 20)
    save_list_names = [''] * 20
    cnt = 0

    for i in save_list:
        save_list_names[cnt] = util.sed_line(path_images,
                                             i).strip().split(',')[0]
        cnt += 1

    ones1 = util.make_ones(generator)
    zeros = util.make_zeros(generator)

    print('training...')
    for epoch in range(pc.EPOCHS):

        # shuffle training instances
        order = range(num_features)
        random.shuffle(order)

        names_order = all_names[order]
        train_gen = True
        train_dis = True

        print('epoch %d' % epoch)
        for step in range(total_steps):
            names = names_order[step * pc.BATCH_SIZE:(step + 1) *
                                pc.BATCH_SIZE]
            features = util.get_features_h5_in_batches(names, train=pc.TRAIN)
            features = util.to_correct_input(features)
            labels_32, labels_224 = util.get_labels(names)
            # labels_32 = util.get_labels(names)
            # vgg16_features = util.get_features_h5_in_batches(names, train=pc.TRAIN, which_features='vgg16')
            # vgg16_features = util.to_correct_input(vgg16_features)
            # labels_32 = np.asarray(labels_32, dtype=np.float32)

            with chainer.using_config('train', train_gen):
                generator.cleargrads()
                prediction = generator(chainer.Variable(features))

            with chainer.using_config('train', train_dis):
                discriminator.cleargrads()
                print('prediction shape', np.shape(prediction.data))
                data = np.reshape(
                    generator(chainer.Variable(features)).data,
                    (pc.BATCH_SIZE, 32, 32, 3))
                data = np.transpose(data, (0, 3, 1, 2))
                fake_prob = discriminator(chainer.Variable(data))

                other_data = np.reshape(labels_32, (pc.BATCH_SIZE, 32, 32, 3))
                other_data = np.transpose(other_data, (0, 3, 1, 2))
                real_prob = discriminator(chainer.Variable(other_data))

                feature_truth = vgg16(labels_224,
                                      layers=['conv3_3'])['conv3_3']
                feature_reconstruction = vgg16(util.fix_prediction_for_vgg16(
                    prediction, vgg16),
                                               layers=['conv3_3'])['conv3_3']
                # feature_reconstruction = None
                # ----------------------------------------------------------------
                # CALCULATE LOSS
                lambda_adv = 10**2
                lambda_sti = 2 * (10**-6)
                lambda_fea = 10**-2
                l_adv = lambda_adv * F.sigmoid_cross_entropy(
                    fake_prob, ones1.data)
                # TODO: mask is probably breaking the graph, fix this
                thing_1 = util.apply_mask(labels_32, mask_L_sti)
                thing_2 = util.apply_mask(prediction.data, mask_L_sti)
                l_sti = lambda_sti * F.mean_squared_error(thing_1, thing_2)
                l_fea = lambda_fea * F.mean_squared_error(
                    feature_truth, feature_reconstruction)
                generator_loss = l_adv + l_sti + l_fea
                generator_loss.backward()
                generator_optimizer.update()
                generator_train_loss[epoch] += generator_loss.data

                lambda_dis = 10**2
                discriminator_loss = lambda_dis * (
                    F.sigmoid_cross_entropy(real_prob, ones1.data) +
                    F.sigmoid_cross_entropy(fake_prob, zeros.data))
                discriminator_loss.backward()
                discriminator_optimizer.update()
                discriminator_train_loss[epoch] += discriminator_loss.data

                # ----------------------------------------------------------------
                # when to suspend / resume training
                dis_adv_ratio = discriminator_loss.data / l_adv.data

                if dis_adv_ratio < 0.1:
                    train_dis = False
                if dis_adv_ratio > 0.5:
                    train_dis = True

                if dis_adv_ratio > 10:
                    train_gen = False
                if dis_adv_ratio < 2:
                    train_gen = True

                # print('%d/%d %d/%d  generator: %f   l_adv: %f  l_sti: %f   discriminator: %f  l3: %f  l4: %f' % (
                # epoch, pc.EPOCHS, step, total_steps, generator_loss.data, l_adv.data, l_sti.data, discriminator_loss.data,
                # l3.data, l4.data))
                print(
                    '%d/%d %d/%d  generator: %f   l_adv: %f  l_sti: %f   l_fea: %f    discriminator: %f    dis/adv: %f'
                    % (epoch, pc.EPOCHS, step, total_steps,
                       generator_loss.data, l_adv.data, l_sti.data, l_fea.data,
                       discriminator_loss.data, dis_adv_ratio))

                # information = util.update_information(information1, step, generator_loss.data, l_adv.data, l_sti.data)
                # information = util.update_information(information2, step, discriminator_loss.data, l3.data, l4.data)

                # visualizing loss
                # prev_max_ax1 = util.plot_everything(information1, fig1, lines1, ax1, prev_max_ax1, step)
                # prev_max_ax2 = util.plot_everything(information2, fig2, lines2, ax2, prev_max_ax2, step)

            with chainer.using_config('train', False):
                for i in range(len(names)):
                    if names[i] in save_list_names:
                        f = np.expand_dims(features[i], 0)
                        prediction = generator(f)
                        util.save_image(prediction, names[i], epoch,
                                        pp.RECONSTRUCTION_FOLDER)
                        print("image '%s' saved" % names[i])

        # if (epoch+1) % pc.SAVE_EVERY_N_STEPS == 0:
        #     util.save_model(generator, epoch)

        generator_train_loss[epoch] /= total_steps
        print(generator_train_loss[epoch])

        discriminator_train_loss[epoch] /= total_steps
        print(discriminator_train_loss[epoch])
Пример #9
0
def save_caffe_as_chainer():
    something = VGG16Layers(pretrained_model=False)
    caffeVGG_as_chainerVGG = 'chainerVGGFace'
    something.convert_caffemodel_to_npz(VGGFACE_CAFFE_MODEL,
                                        caffeVGG_as_chainerVGG)
Пример #10
0
import copy

shared = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), 'shared')
sys.path.append(shared)

from utils.dataset import KomeDataset
from utils.functions import gram_matrix, compact_bilinear_pooling
from utils import debugger

parser = argparse.ArgumentParser(description='extract patch from TCGA data')
parser.add_argument('--gpu', '-g', type=int, default=0)
args = parser.parse_args()

base_dir = './'

vgg16 = VGG16Layers()
cuda.get_device_from_id(args.gpu).use()
vgg16.to_gpu()

cbp_sizes = [256, 1024, 4096]
randweight_dict = {}
for cbp_size in cbp_sizes:
    randweight = np.load(os.path.join(shared, 'cbp/randweight_256_to_{}.npz'.format(cbp_size)))
    randweight_dict[cbp_size] = {'W1': cuda.to_gpu(randweight['W1']), 'W2': cuda.to_gpu(randweight['W2'])}

batch_size = 256
image_size = 256
vggl = 'pool3'
norm_type = 'powerl2'
cbp_size = 1024