import time
import math

from data import TrainingData
from imagedatapreprocessing import DatasetProcessing
from torchvision import models, transforms
from torch.utils.data import Dataset, DataLoader
from PIL import Image

# Use CUDA if available -> replacing every .cuda() with .to(device)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

# LOAD IMAGES
print("Loading data")
td = TrainingData()
image_train, y_train, image_val, y_val, output_size = td.getTrainingImages("fashion")

print("Output Size:", output_size)
print('Train on', len(image_train), 'Test on', len(image_val))
print(image_train[0])

######################### DATA PREPROCESSING ########################
batch_size = 300
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])

train_transform = transforms.Compose([
								transforms.ToPILImage(),
								transforms.Resize((128, 128)),
								transforms.RandomHorizontalFlip(),
								transforms.RandomRotation(60),
示例#2
0
import time
import math

from data import TrainingData
from imagedatapreprocessing import DatasetProcessing
from torchvision import models, transforms
from torch.utils.data import Dataset, DataLoader
from PIL import Image

# Use CUDA if available -> replacing every .cuda() with .to(device)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

# LOAD IMAGES
print("Loading data")
td = TrainingData()
image_train, y_train, image_val, y_val, output_size = td.getTrainingImages("mobile")

print("Output Size:", output_size)
print('Train on', len(image_train), 'Test on', len(image_val))
print(image_train[0])

######################### DATA PREPROCESSING ########################
batch_size = 200
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])

train_transform = transforms.Compose([
								transforms.ToPILImage(),
								transforms.RandomAffine(degrees = 60, 
														translate = (0.2, 0.2), 
														scale = (0.7, 1.3)),
示例#3
0
    parser.add_argument('--use_augmentation',
                        required=False,
                        default=False,
                        help='Use image augmentation. Default false.')
    parser.add_argument(
        '--model_dir',
        required=False,
        default=GlobalConfig.get('MODEL_DIR'),
        metavar="/path/to/logs/",
        help='Logs and checkpoints directory (default=/projects/lungbox/models)'
    )
    args = parser.parse_args()

    # Set up training data
    elapsed_start = time.perf_counter()
    data = TrainingData(subset_size=int(args.subset_size),
                        validation_split=float(args.validation_split))
    elapsed_end = time.perf_counter()
    logger.info('Data Setup Time: %ss' % round(elapsed_end - elapsed_start, 4))

    # Set up Mask R-CNN model
    elapsed_start = time.perf_counter()
    model = modellib.MaskRCNN(mode='training',
                              config=DetectorConfig(),
                              model_dir=args.model_dir)
    elapsed_end = time.perf_counter()
    logger.info('Model Setup Time: %ss' %
                round(elapsed_end - elapsed_start, 4))

    # Add image augmentation params
    if args.use_augmentation:
        augmentation = iaa.SomeOf(
import time
import math

from data import TrainingData
from imagedatapreprocessing import DatasetProcessing
from torchvision import models, transforms
from torch.utils.data import Dataset, DataLoader
from PIL import Image

# Use CUDA if available -> replacing every .cuda() with .to(device)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# device = torch.device('cpu') # use this if gpu doesnt provide sufficient memory

# LOAD IMAGES
print("Loading data")
td = TrainingData()
image_train, y_train, image_val, y_val, output_size = td.getTrainingImages("beauty")
print("Output Size:", output_size)
print('Train on', len(image_train), 'Test on', len(image_val))

def train(train_loader, model):
	global current_val_acc
	acc_l = []
	loss_l = []
	model.train()
	start = time.time()
	for i, (x, y) in enumerate(train_loader):
		x, y = x.to(device), y.to(device)

		model.zero_grad()
		output = model(x)
示例#5
0
def main():
    data = get_train_data()

    # Train data
    train, val = train_test_split(data, test_size=200, random_state=1)
    train += train
    train += train
    train += train
    len(train),len(val)

    # Valid data
    val_a = np.zeros((len(val),)+config.img_shape,
                     dtype=K.floatx()) # Preprocess validation images
    val_b = np.zeros((len(val),4),dtype=K.floatx()) # Preprocess bounding boxes
    for i,(p,coords) in enumerate(tqdm(val)):
        img,trans      = read_for_validation(p)
        coords         = coord_transform(coords, mat_inv(trans))
        x0,y0,x1,y1    = bounding_rectangle(coords, img.shape)
        val_a[i,:,:,:] = img
        val_b[i,0]     = x0
        val_b[i,1]     = y0
        val_b[i,2]     = x1
        val_b[i,3]     = y1

    # Train using cyclic learning rate
    for num in range(1, 4):
        model_name = 'cropping-%01d.h5' % num
    model = build_model()
    print(model_name)
    model.compile(Adam(lr=0.032), loss='mean_squared_error')
    model.fit_generator(
        TrainingData(train), epochs=50, max_queue_size=12, workers=4, verbose=1,
        validation_data=(val_a, val_b),
        callbacks=[
            EarlyStopping(monitor='val_loss', patience=9, min_delta=0.1, verbose=1),
            ReduceLROnPlateau(monitor='val_loss', patience=3, min_delta=0.1, factor=0.25, min_lr=0.002, verbose=1),
            ModelCheckpoint(model_name, save_best_only=True, save_weights_only=True),
        ])
    model.load_weights(model_name)
    model.evaluate(val_a, val_b, verbose=0)

    # Now choose which model to use
    model.load_weights('cropping-1.h5')
    loss1 = model.evaluate(val_a, val_b, verbose=0)
    model.load_weights('cropping-2.h5')
    loss2 = model.evaluate(val_a, val_b, verbose=0)
    model.load_weights('cropping-3.h5')
    loss3 = model.evaluate(val_a, val_b, verbose=0)
    model_name = 'cropping-1.h5'
    if loss2 <= loss1 and loss2 < loss3: model_name = 'cropping-2.h5'
    if loss3 <= loss1 and loss3 <= loss2: model_name = 'cropping-3.h5'
    model.load_weights(model_name)

    # Variance normalization
    model2 = build_model(with_dropout=False)
    model2.load_weights(model_name)
    model2.compile(Adam(lr=0.002), loss='mean_squared_error')
    model2.evaluate(val_a, val_b, verbose=0)

    # Recompute the mean and variance running average without dropout
    for layer in model2.layers:
        if not isinstance(layer, BatchNormalization):
            layer.trainable = False
    model2.compile(Adam(lr=0.002), loss='mean_squared_error')
    model2.fit_generator(TrainingData(), epochs=1, max_queue_size=12, workers=6, verbose=1, validation_data=(val_a, val_b))
    for layer in model2.layers:
        if not isinstance(layer, BatchNormalization):
            layer.trainable = True
    model2.compile(Adam(lr=0.002), loss='mean_squared_error')
    model2.save('cropping.model')

    # Generate bounding boxes
    tagged = [p for _,p,_ in pd.read_csv(config.train_csv).to_records()]
    submit = [p for _,p,_ in pd.read_csv(config.sample_submission).to_records()]
    join = tagged + submit

    # If the picture is part of the bounding box dataset, use the golden value.
    p2bb = {}
    for i,(p,coords) in enumerate(data): p2bb[p] = bounding_rectangle(coords, read_raw_image(p).size)
    len(p2bb)

    # For other pictures, evaluate the model.
    p2bb = {}
    for p in tqdm(join):
        if p not in p2bb:
            img,trans         = read_for_validation(p)
            a                 = np.expand_dims(img, axis=0)
            x0, y0, x1, y1    = model2.predict(a).squeeze()
            (u0, v0),(u1, v1) = coord_transform([(x0,y0),(x1,y1)], trans)
            img               = read_raw_image(p)
            u0 = max(u0, 0)
            v0 = max(v0, 0)
            u1 = min(u1, img.size[0])
            v1 = min(v1, img.size[1])
            p2bb[p]           = (u0, v0, u1, v1)

    with open('./input/cropping.csv', 'w') as f:
        f.write('Image, x0, y0, x1, y1\n')
        for p in p2bb:
            u0, v0, u1, v1 = p2bb[p]
            f.write('{},{},{},{},{}\n'.format(str(p),
                                              str(u0),
                                              str(v0),
                                              str(u1),
                                              str(v1)))
        self.fc = nn.Linear(hidden_size * 2, output_size).to(device)
        self.hidden = self.initHidden()

    def forward(self, input):
        embed = self.embeddings(input)
        embed = embed.unsqueeze(1)
        lstm_out, _ = self.lstm(embed, self.hidden)
        out = F.log_softmax(self.fc(lstm_out[-1]), dim=1).to(device)
        return out

    def initHidden(self):
        return (torch.zeros(2, 1, self.hidden_size).to(device),
                torch.zeros(2, 1, self.hidden_size).to(device))


td = TrainingData(validation_percent)

for cat in ["beauty", "mobile", "fashion"]:
    x_train, y_train, x_val, y_val, output_size = td.getTrainingData(cat)

    total_samples = len(y_train) + len(y_val)

    print(cat)

    if cat == "fashion":
        model = LSTM(embed, 100, 150, output_size)
    else:
        model = LSTM(embed, 100, 100, output_size)
    model.load_state_dict(torch.load("models/" + cat + ".text.pth"))
    model.to(device)
    for param in model.parameters():
示例#7
0
        self.fc = nn.Linear(hidden_size * 2, output_size).to(device)
        self.hidden = self.initHidden()

    def forward(self, input):
        embed = self.embeddings(input)
        embed = embed.unsqueeze(1)
        lstm_out, _ = self.lstm(embed, self.hidden)
        out = F.log_softmax(self.fc(lstm_out[-1]), dim=1).to(device)
        return out

    def initHidden(self):
        return (torch.zeros(2, 1, self.hidden_size).to(device), torch.zeros(2, 1, self.hidden_size).to(device))

cats = ["beauty", "mobile", "fashion"]
models = []
td = TrainingData(0.1)
for cat in cats:
    _, _, _, _, output_size = td.getTrainingData(cat)
    if cat == "fashion":
        model = LSTM(embed, 100, 150, output_size)
    else: 
        model = LSTM(embed, 100, 100, output_size)
    model.load_state_dict(torch.load("models/"+cat+".text.pth"))
    model.to(device)
    for param in model.parameters():
        param.requires_grad = False

    model.eval()
    models.append(model)

data = pd.read_csv("data/test.csv", delimiter=",")
示例#8
0
# build the driver
driver = Driver((80, 80, 1))
driver.build(ModelType.CONV1)

##################### Initial training ####################
initial_data = [
    './data/trk1_normal_1',
    './data/trk1_normal_2',
    './data/trk1_normal_3',
    './data/trk1_corner_infill',
    './data/udacity_data',
]

# 1) The initial training step
data = TrainingData(initial_data)
driver.trainGen(data.training_generator,
                data.training_size,
                5,
                data.validation_generator,
                data.validation_size,
                lr=0.001)

####################### Fine tuning #######################
fine_tune_data = [
    './data/trk1_normal_1',
    './data/trk1_normal_2',
    './data/trk1_normal_3',
    './data/trk1_normal_4',
    './data/trk1_swerve_fill',
    './data/trk1_corner_infill',