Best Python code snippet using localstack_python
do_solve.py
Source:do_solve.py  
1def do_solve(niter, solver, disp_interval, test_interval, test_iters, training_id, batch_size):2    """Run solvers for niter iterations,3       returning the loss and recorded each iteration.4       `solvers` is a list of (name, solver) tuples."""5    import tempfile6    import numpy as np7    import os8    from pylab import zeros, arange, subplots, plt, savefig9    import glob10    import time11    # SET PLOTS DATA12    # train_loss = zeros(niter/disp_interval)13    train_loss_r = zeros(niter/disp_interval)14    train_correct_pairs = zeros(niter/disp_interval)15    # train_acc = zeros(niter/disp_interval)16    # val_loss = zeros(niter/test_interval)17    val_loss_r = zeros(niter/test_interval)18    val_correct_pairs = zeros(niter/test_interval)19    # val_acc = zeros(niter/test_interval)20    it_axes = (arange(niter) * disp_interval) + disp_interval21    it_val_axes = (arange(niter) * test_interval) + test_interval22    _, ax1 = subplots()23    ax2 = ax1.twinx()24    ax1.set_xlabel('iteration')25    ax1.set_ylabel('train loss (r), val loss (g),')# train loss_r (c), val loss_r (o)')26    ax2.set_ylabel('train correct pairs (b) val correct pairs (m)')# train top1 (y) val top1 (bk)')27    ax2.set_autoscaley_on(False)28    ax2.set_ylim([0, batch_size])29    # loss = {name: np.zeros(niter) for name, _ in solvers}30    loss_r = np.zeros(niter)31    correct_pairs = np.zeros(niter)32    # acc = {name: np.zeros(niter) for name, _ in solvers}33    lowest_val_loss = 100034    best_it = 035    #RUN TRAINING36    for it in range(niter):37        # start = time.time()38        solver.step(1)  # run a single SGD step in Caffe39        # end = time.time()40        # print "Time step: " + str((end - start))41        # print "Max before ReLU: " + str(np.max(s.net.blobs['inception_5b/pool_proj'].data))42        # print "Max last FC: " + str(np.max(s.net.blobs['loss3/classifierCustom'].data))43        #loss[name][it] = s.net.blobs['loss3/loss3/classification'].data.copy()44        loss_r[it] = solver.net.blobs['loss3/loss3/ranking'].data.copy()45        correct_pairs[it] = solver.net.blobs['correct_pairs'].data.copy()46        # acc[name][it] = s.net.blobs['loss3/top-1'].data.copy()47        #PLOT48        if it % disp_interval == 0 or it + 1 == niter:49            # loss_disp = 'loss=' + str(loss['my_solver'][it]) + ' correct_pairs=' + str(correct_pairs['my_solver'][it]) + ' loss ranking=' + str(loss_r['my_solver'][it])50            loss_disp = ' correct_pairs=' + str(correct_pairs[it]) + ' loss ranking=' + str(loss_r[it])51            print '%3d) %s' % (it, loss_disp)52            # train_loss[it/disp_interval] = loss[it]53            train_loss_r[it/disp_interval] = loss_r[it]54            train_correct_pairs[it/disp_interval] = correct_pairs[it]55            # train_acc[it/disp_interval] = acc[it] *12056            # ax1.plot(it_axes[0:it/disp_interval], train_loss[0:it/disp_interval], 'r')57            ax1.plot(it_axes[0:it/disp_interval], train_loss_r[0:it/disp_interval], 'c')58            ax2.plot(it_axes[0:it/disp_interval], train_correct_pairs[0:it/disp_interval], 'b')59            # ax2.plot(it_axes[0:it/disp_interval], train_acc[0:it/disp_interval], 'gold')60            # if it > test_interval:61            #     ax1.plot(it_val_axes[0:it/test_interval], val_loss[0:it/test_interval], 'g') #Val always on top62            ax1.set_ylim([0,2])63            plt.title(training_id)64            plt.ion()65            plt.grid(True)66            plt.show()67            plt.pause(0.001)68            # title = '../training/numbers/training-' + str(it) + '.png'  # Save graph to disk69            # savefig(title, bbox_inches='tight')70        #VALIDATE71        if it % test_interval == 0 and it > 0:72            # loss_val = 073            loss_val_r = 074            cur_correct_pairs = 075            # cur_acc = 076            for i in range(test_iters):77                solver.test_nets[0].forward()78                # loss_val += solver.test_nets[0].blobs['loss3/loss3/classification'].data79                loss_val_r += solver.test_nets[0].blobs['loss3/loss3/ranking'].data80                cur_correct_pairs += solver.test_nets[0].blobs['correct_pairs'].data81                # cur_acc += solvers[0][1].test_nets[0].blobs['loss3/top-1'].data82            # loss_val /= test_iters83            loss_val_r /= test_iters84            cur_correct_pairs /= test_iters85            # cur_acc /= test_iters86            # cur_acc *= 12087            # print("Val loss: " + str(loss_val) + " Val correct pairs: " + str(cur_correct_pairs) + " Val loss ranking: " + str(loss_val_r) + "Val acc: "+ str(cur_acc))88            print(" Val correct pairs: " + str(cur_correct_pairs) + " Val loss ranking: " + str(loss_val_r))89            # val_loss[it/test_interval - 1] = loss_val90            val_loss_r[it/test_interval - 1] = loss_val_r91            val_correct_pairs[it/test_interval - 1] = cur_correct_pairs92            # val_acc[it/test_interval - 1] = cur_acc93            # ax1.plot(it_val_axes[0:it/test_interval], val_loss[0:it/test_interval], 'g')94            ax1.plot(it_val_axes[0:it/test_interval], val_loss_r[0:it/test_interval], 'orange')95            ax2.plot(it_val_axes[0:it/test_interval], val_correct_pairs[0:it/test_interval], 'm')96            # ax2.plot(it_val_axes[0:it/test_interval], val_acc[0:it/test_interval], 'k')97            ax1.set_ylim([0,2])98            ax1.set_xlabel('iteration ' + 'Best it: ' + str(best_it) + ' Best Val Loss: ' + str(int(lowest_val_loss)))99            plt.title(training_id)100            plt.ion()101            plt.grid(True)102            plt.show()103            plt.pause(0.001)104            title = '../../../hd/datasets/instaFashion/models/training/' + training_id + str(it) + '.png'  # Save graph to disk105            savefig(title, bbox_inches='tight')106            if loss_val_r < lowest_val_loss:107                print("Best Val loss!")108                lowest_val_loss = loss_val_r109                best_it = it110                filename = '../../../hd/datasets/instaFashion/models/CNNContrastive/' + training_id + 'best_valLoss_' + str(111                    int(loss_val_r)) + '_it_' + str(it) + '.caffemodel'112                prefix = 30113                for cur_filename in glob.glob(filename[:-prefix] + '*'):114                    print(cur_filename)115                    os.remove(cur_filename)...__init__.py
Source:__init__.py  
1# import numpy as np2# import PIL.Image3# from dream_utils import *4# resize_in = (224, 224)5# resize_out = (700, 700)6# Athena squared7# image = np.float32(PIL.Image.open('images/athena_louvre_700px.jpg'))8# image_mask = PIL.Image.open('images/athena_louvre_700px_face_mask.png')9# Louise10# image = np.float32(PIL.Image.open('images/louise.jpg'))11# image_mask = PIL.Image.open('images/louise_crop_mask.png')12TIME_FORMAT = "%Y-%m-%d_%H:%M:%S.%f"13SOLVERS = [14    # {'name': '0010', 'snapshot':  1,  'max_iter':   10, 'base_lr': 0.0001, 'test_interval': 10},  # 015    {'snapshot':   1, 'max_iter':   20, 'base_lr': 0.0001, 'test_interval': 20},  # 116    {'snapshot':   1, 'max_iter':   40, 'base_lr': 0.001,  'test_interval': 20},  # 217    {'snapshot':   1, 'max_iter':   80, 'base_lr': 0.01,   'test_interval': 40},  # 318    {'snapshot':   1, 'max_iter':  120, 'base_lr': 0.01,   'test_interval': 20},  # 419    {'snapshot':   1, 'max_iter':  160, 'base_lr': 0.01,   'test_interval': 20},  # 520    {'snapshot':   1, 'max_iter':  200, 'base_lr': 0.01,   'test_interval': 20},  # 621    {'snapshot':   1, 'max_iter':  220, 'base_lr': 0.02,   'test_interval': 10},  # 722    {'snapshot':   1, 'max_iter':  240, 'base_lr': 0.02,   'test_interval': 10},  # 823    {'snapshot':   1, 'max_iter':  250, 'base_lr': 0.03,   'test_interval': 10},  # 924    {'snapshot':   1, 'max_iter':  260, 'base_lr': 0.01,   'test_interval': 10},  # 1025    {'snapshot':   1, 'max_iter':  270, 'base_lr': 0.0005, 'test_interval': 10},  # 1126    {'snapshot':   1, 'max_iter':  290, 'base_lr': 0.001,  'test_interval': 10},  # 1227    {'snapshot':   5, 'max_iter':  340, 'base_lr': 0.01,   'test_interval': 10},  # 1328    {'snapshot':  20, 'max_iter':  480, 'base_lr': 0.01,   'test_interval': 100}, # 1429    {'snapshot':  20, 'max_iter':  680, 'base_lr': 0.01,   'test_interval': 100}, # 1530    {'snapshot':  20, 'max_iter':  880, 'base_lr': 0.01,   'test_interval': 100}, # 1631    {'snapshot':  20, 'max_iter': 1080, 'base_lr': 0.01,   'test_interval': 100}, # 1732    {'snapshot':  40, 'max_iter': 1200, 'base_lr': 0.01,   'test_interval': 100}, # 1833    {'snapshot':  40, 'max_iter': 1400, 'base_lr': 0.005,  'test_interval': 100}, # 1934    {'snapshot': 100, 'max_iter': 2200, 'base_lr': 0.005,  'test_interval': 200}, # 2035    {'snapshot': 100, 'max_iter': 3000, 'base_lr': 0.005,  'test_interval': 200}, # 2136    {'snapshot':   1, 'max_iter': 3020, 'base_lr': 0.001,  'test_interval': 20, 'gamma': 1.0}, # 2237    {'snapshot':   1, 'max_iter': 3040, 'base_lr': 0.01,   'test_interval': 20, 'gamma': 1.0}, # 2338    {'snapshot':   1, 'max_iter': 3060, 'base_lr': 0.02,   'test_interval': 20, 'gamma': 1.0}, # 2439    {'snapshot':   1, 'max_iter': 3080, 'base_lr': 0.015,  'test_interval': 20, 'gamma': 1.0}, # 2540    {'snapshot':   1, 'max_iter': 3100, 'base_lr': 0.01,   'test_interval': 20, 'gamma': 1.0}, # 2641    {'snapshot':   1, 'max_iter': 3150, 'base_lr': 0.005,  'test_interval': 25, 'gamma': 1.0}, # 2742    {'snapshot':   1, 'max_iter': 3170, 'base_lr': 0.005,  'test_interval': 20, 'gamma': 1.0}, # 2843    {'snapshot':   1, 'max_iter': 3200, 'base_lr': 0.005,  'test_interval': 20, 'gamma': 1.0}, # 2944    {'snapshot':  10, 'max_iter': 3300, 'base_lr': 0.001,  'test_interval': 20, 'gamma': 1.0}, # 3045    {'snapshot':  25, 'max_iter': 3500, 'base_lr': 0.0005, 'test_interval': 100, 'gamma': 1.0}, # 3146]47# default solver name equals to max_iter48for solver in SOLVERS:49    if not solver.has_key('name'):50        solver['name'] = '%s' % solver['max_iter']51RIA_MODEL_DIR = 'models/Ria_Gurtow/'52RIA_MODEL_SNAPSHOTS_PREFIX = 'models/Ria_Gurtow/generations/ria_gurtow_iter_'53EMOTIONS_MODEL = 'models/VGG_S_rgb/EmotiW_VGG_S.caffemodel'...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
