text
stringlengths
1
93.6k
'Accuracy {accs.val:.3f} ({accs.avg:.3f})'.format(i_batch, len(val_loader),
batch_time=batch_time,
loss=losses,
accs=accs))
return accs.avg, losses.avg
def main(args):
superclass = args['superclass']
if superclass is None:
superclass = 'Animals'
train_loader = DataLoader(dataset=ZslDataset(superclass, 'train'), batch_size=batch_size, shuffle=True,
pin_memory=True, drop_last=True)
val_loader = DataLoader(dataset=ZslDataset(superclass, 'valid'), batch_size=batch_size, pin_memory=True,
drop_last=True)
embedding_size = get_embedding_size_by_superclass(superclass)
print('embedding_size: ' + str(embedding_size))
W = torch.randn(feature_size, embedding_size, requires_grad=True, device=device)
torch.nn.init.xavier_uniform_(W)
attributes_per_class = get_attributes_per_class_by_superclass(superclass)
# Initialize encoder
model = Encoder(embedding_size=embedding_size)
# Use appropriate device
model = model.to(device)
# Initialize optimizers
optimizer = optim.Adam([{'params': model.parameters()}, {'params': W}], lr=learning_rate)
best_acc = 0
epochs_since_improvement = 0
# Epochs
for epoch in range(start_epoch, epochs):
# Decay learning rate if there is no improvement for 8 consecutive epochs, and terminate training after 20
if epochs_since_improvement == 20:
break
if epochs_since_improvement > 0 and epochs_since_improvement % 8 == 0:
adjust_learning_rate(optimizer, 0.8)
# One epoch's training
train(epoch, train_loader, model, W, optimizer, attributes_per_class)
# One epoch's validation
val_acc, val_loss = valid(val_loader, model, W, attributes_per_class)
print('\n * ACCURACY - {acc:.3f}, LOSS - {loss:.3f}\n'.format(acc=val_acc, loss=val_loss))
# Check if there was an improvement
is_best = val_acc > best_acc
best_acc = max(best_acc, val_acc)
if not is_best:
epochs_since_improvement += 1
print("\nEpochs since last improvement: %d\n" % (epochs_since_improvement,))
else:
epochs_since_improvement = 0
# Save checkpoint
save_checkpoint(epoch, model, W, optimizer, val_acc, is_best, superclass)
if __name__ == '__main__':
# Parse arguments
ap = argparse.ArgumentParser()
ap.add_argument("-s", "--superclass",
help="superclass ('Animals', 'Fruits', 'Vehicles', 'Electronics', 'Hairstyles')")
args = vars(ap.parse_args())
main(args)
# <FILESEP>
#!/usr/bin/env python
#_MIT License
#_
#_Copyright (c) 2017 Dan Persons (dpersonsdev@gmail.com)
#_
#_Permission is hereby granted, free of charge, to any person obtaining a copy
#_of this software and associated documentation files (the "Software"), to deal
#_in the Software without restriction, including without limitation the rights
#_to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#_copies of the Software, and to permit persons to whom the Software is
#_furnished to do so, subject to the following conditions:
#_
#_The above copyright notice and this permission notice shall be included in all
#_copies or substantial portions of the Software.
#_
#_THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#_IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#_FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#_AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#_LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#_OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#_SOFTWARE.
from siemstress.triggercore import SiemTriggerCore