Ejemplo n.º 1
0
    def merge_with_cifar(self):
        """
        merges ocr data with cifar data and relabels in order to perform binary classification.
        This method is in charge of generating a unique data set merging 50000 images containing text (from the OCR data set)
        and 50000 images not containing text (from the CIFAR-10 data set).
        """ 
        cifar = Cifar('/home/francesco/Dropbox/DSR/OCR/cifar-config.py')
        
        text = OcrData('/home/francesco/Dropbox/DSR/OCR/text-config.py')
        
        text.ocr['target'][:] = 1

        total = 100000
        seed(10)
        k = sample(range(total), total)
        
        cifar_plus_text = {
                           'images': np.concatenate((cifar.cif['images'], text.ocr['images'][:50000]))[k],
                           'data': np.concatenate((cifar.cif['data'], text.ocr['data'][:50000]))[k],
                           'target': np.concatenate((cifar.cif['target'], text.ocr['target'][:50000]))[k]
                           }
 
        now = str(datetime.now()).replace(':','-')   
        fname_out = 'images-{}-{}-{}.pickle'.format(cifar_plus_text['target'].shape[0], self.img_size, now)
        full_name = os.path.join(self.folder_data,fname_out)
        with open(full_name, 'wb') as fout:
            cPickle.dump(cifar_plus_text, fout, -1)
            
        return cifar_plus_text
Ejemplo n.º 2
0
no_of_epochs = 10
dropout_rate = 0.8

y = tf.placeholder(tf.float32, [None, model.n_classes])

cost = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(logits=model.out, labels=y))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
correct_pred = tf.equal(tf.argmax(model.out, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

tf.summary.histogram("cost", cost)  # <- Added this summary of cost
tf.summary.histogram("accuracy", accuracy)  # <- Added this summary of accuracy

cifar = Cifar(batch_size=batch_size)

init = tf.initialize_all_variables()

saver = tf.train.Saver()
i = 0  # <- Initialize i, for writer
with tf.Session() as sess:

    sess.run(init)

    # Create reader and writer, which writes to ./logs folder
    reader = tf.WholeFileReader()
    writer = tf.summary.FileWriter('./logs/', sess.graph)
    for epoch in range(no_of_epochs):
        for batch in tqdm(cifar.batches,
                          desc="Epoch {}".format(epoch),
Ejemplo n.º 3
0
image_size = 224

conv5 = tf.layers.flatten(pretrained.maxpool5)  # tf.flatten

weights = tf.Variable(tf.zeros([9216, n_classes]), name="output_weight")
bias = tf.Variable(tf.truncated_normal([n_classes]), name="output_bias")
model = tf.matmul(conv5, weights) + bias

outputs = tf.placeholder(tf.float32, [None, n_classes])

cost = tf.losses.softmax_cross_entropy(outputs, model)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
correct_pred = tf.equal(tf.argmax(model, 1), tf.argmax(outputs, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

cifar = Cifar(batch_size=batch_size)
cifar.create_resized_test_set(dim=n_classes)

init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)
    run_options = tf.RunOptions(report_tensor_allocations_upon_oom=True)

    for epoch in range(no_of_epochs):
        for i in tqdm(range(cifar.no_of_batches),
                      desc="Epoch {}".format(epoch),
                      unit=" batch "):
            this_batch = cifar.batch(i)
            input_batch, out = helper.reshape_batch(this_batch,
                                                    (image_size, image_size),
                                                    n_classes)
Ejemplo n.º 4
0
                        help="L2 weight decay.")
    parser.add_argument("--width_factor",
                        default=8,
                        type=int,
                        help="How many times wider compared to normal ResNet.")
    parser.add_argument("--train_size",
                        default=50000,
                        type=int,
                        help="How many training samples to use.")
    args = parser.parse_args()
    print(args)

    initialize(args, seed=42)
    #    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    dataset = Cifar(args.batch_size, args.threads, args.train_size)
    log = Log(log_each=10)
    #    model = WideResNet(args.depth, args.width_factor, args.dropout, in_channels=3, labels=10).to(device)
    model = WideResNet(args.depth,
                       args.width_factor,
                       args.dropout,
                       in_channels=3,
                       labels=10)
    model = nn.DataParallel(model).cuda()

    base_optimizer = torch.optim.SGD
    optimizer = SAM(model.parameters(),
                    base_optimizer,
                    rho=args.rho,
                    lr=args.learning_rate,
                    momentum=args.momentum,
Ejemplo n.º 5
0
print('Load pretrained model...')

# extracted features
extractor = tf.layers.flatten(model.conv5)

# classifier
#weights = tf.Variable(tf.zeros([9216, n_classes]), name="output_weight")
#bias = tf.Variable(tf.truncated_normal([n_classes]), name="output_bias")
#model = tf.matmul(extractor, weights) + bias
#outputs = tf.placeholder(tf.float32, [None, n_classes])

# ====================
# config dataset
# ====================
print('Prepare dataset')
train_dataset = Cifar(batch_size = batch_size)

init = tf.initialize_all_variables()

trian_features = None
train_label = None
with tf.Session(config=config) as sess:
    sess.run(init)
    for i in tqdm(range(20), unit=" batch "):
        this_batch = train_dataset.batch(i)
        train_X, train_y = helper.reshape_batch(this_batch, (image_size, image_size), n_classes)
        train_y = [np.argmax(element) for element in train_y]
        features = sess.run(
            [extractor],
            feed_dict={
                model.input_images: train_X 
Ejemplo n.º 6
0
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

from cifar import Cifar
from model import Model

cifar_data = Cifar('../../../cifar-10-batches-py/cifar-10-batches-py')
cifar_data.convert_to_lab()

sess = tf.Session()
model = Model(sess=sess,
              data=cifar_data.data,
              val_data=cifar_data.val_data,
              test_data=cifar_data.test_data,
              cifar=cifar_data,
              num_iter=10000,
              sup_learning_rate=1e-2,
              uns_learning_rate_1=1e-2,
              uns_learning_rate_2=1e-2,
              batch_size=16,
              test_size=16,
              is_supervised=True,
              is_untrained=False)

model.count_variables()
model.change_sup_percentage(10)
model.train_init()
model.train()
model.test()
Ejemplo n.º 7
0
                        type=int,
                        help="Train using multiple GPUs")
    parser.add_argument("--loss",
                        default=0,
                        type=int,
                        help="= 0, smooth CE; = 1, focal loss.")
    parser.add_argument("--data_bal",
                        default='equal',
                        type=str,
                        help="Set to 'equal' (default) or 'unequal'.")
    args = parser.parse_args()
    print(args)

    initialize(args, seed=42)

    dataset = Cifar(args)

    log = Log(log_each=10)
    if args.multigpu == 1:
        model = WideResNet(args.depth,
                           args.width_factor,
                           args.dropout,
                           in_channels=3,
                           labels=10)
        model = nn.DataParallel(model).cuda()
    else:
        model = WideResNet(args.depth,
                           args.width_factor,
                           args.dropout,
                           in_channels=3,
                           labels=10).cuda()
Ejemplo n.º 8
0
import sys
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint
from keras.utils import np_utils
from cifar import Cifar
from keras import backend as K

num_train_images = 50000
num_test_images = 10000

print(keras.__version__)

# Load CIFAR-10 data.
c = Cifar(num_train_images, num_test_images)
((train_data, train_labels), (test_data, test_labels)) = c.load_data()

# Make Keras happy.

# Reshape and convert to float in range [0, 1] for Keras.
train_data = train_data.reshape(train_data.shape[0], 32, 32, 3)
train_data = train_data.astype('float32')
train_data /= 255

# Check the shape.
print("train_data.shape = %s" % str(train_data.shape))

# Convert from Python list to categorical.
train_labels = np_utils.to_categorical(train_labels)
Ejemplo n.º 9
0
fc6 = pretrained.fc6
weights = tf.Variable(tf.zeros([4096, n_classes]), name="output_weight")
bias = tf.Variable(tf.truncated_normal([n_classes]), name="output_bias")
fc7 = tf.nn.relu_layer(fc6, weights, bias)
model = tf.nn.softmax(fc7)

outputs = tf.placeholder(tf.int32, [None, n_classes])
centroids = tf.get_variable('c',shape=[n_classes],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer(),trainable=False)
centroids_delta = tf.get_variable('centroidsUpdateTempVariable',shape=[n_classes],dtype=tf.float32,initializer=tf.zeros_initializer(),trainable=False)
centroids_batch = tf.gather(centroids,outputs)
cost = tf.nn.l2_loss(model - centroids_batch) / float(batch_size)
#cost = tf.losses.softmax_cross_entropy(outputs, model)+*cLoss
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
correct_pred = tf.equal(tf.argmax(model, 1), tf.argmax(outputs, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
cifar = Cifar(batch_size=batch_size)

init = tf.initialize_all_variables()
saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(init)
    run_options = tf.RunOptions(report_tensor_allocations_upon_oom = True)

    for epoch in range(no_of_epochs):
        for i in tqdm(range(cifar.no_of_batches),
                desc="Epoch {}".format(epoch),
                unit=" batch "):
            this_batch = cifar.batch(i)
            input_batch, out = helper.reshape_batch(this_batch, (image_size, image_size), n_classes)

            
Ejemplo n.º 10
0
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

from cifar import Cifar
from model import Model

cifar_data = Cifar('../hw3/cifar10')
cifar_data.convert_to_lab()

sess = tf.Session()
model = Model(sess=sess,
              data=cifar_data.data,
              val_data=cifar_data.val_data,
              test_data=cifar_data.test_data,
              cifar=cifar_data,
              num_iter=3000,
              sup_learning_rate=1e-2,
              uns_learning_rate_1=1e-2,
              uns_learning_rate_2=1e-2,
              batch_size=16,
              test_size=16,
              is_supervised=True,
              is_untrained=False)

model.count_variables()
model.change_sup_percentage(10)
model.train_init()
model.train()
model.test()
import torchvision
from torch.utils.data import DataLoader
from cifar import Cifar
from resnet import Resnet
# from torchvision.models import resnet18
from utils import Flatten

batchsz = 64
lr = 1e-3
epochs = 50

device = torch.device('cuda')
torch.manual_seed(1234)

train_db = Cifar('D:/phython_work/cifar/cifar-10-batches-py/train_canny',
                 224,
                 mode='train')
val_db = Cifar('D:/phython_work/cifar/cifar-10-batches-py/test_canny',
               224,
               mode='val')
test_db = Cifar('D:/phython_work/cifar/cifar-10-batches-py/test_canny',
                224,
                mode='test')
train_loader = DataLoader(train_db,
                          batch_size=batchsz,
                          shuffle=True,
                          num_workers=4)
val_loader = DataLoader(val_db,
                        batch_size=batchsz,
                        shuffle=True,
                        num_workers=4)