Best Python code snippet using fMBT_python
test_one.py
Source:test_one.py  
1from __future__ import print_function2import argparse3import os4import random5import torch6import torch.nn as nn7import torch.nn.parallel8import torch.backends.cudnn as cudnn9import torch.optim as optim10import torch.utils.data11import torchvision.datasets as dset12import torchvision.transforms as transforms13import torchvision.utils as vutils14from torch.autograd import Variable15from model import _netG16import utils17parser = argparse.ArgumentParser()18parser.add_argument('--dataset', default='streetview', help='cifar10 | lsun | imagenet | folder | lfw ')19parser.add_argument('--test_image', required=True, help='path to dataset')20parser.add_argument('--workers', type=int, help='number of data loading workers', default=4)21parser.add_argument('--batchSize', type=int, default=64, help='input batch size')22parser.add_argument('--imageSize', type=int, default=128, help='the height / width of the input image to network')23parser.add_argument('--nz', type=int, default=100, help='size of the latent z vector')24parser.add_argument('--ngf', type=int, default=64)25parser.add_argument('--ndf', type=int, default=64)26parser.add_argument('--nc', type=int, default=3)27parser.add_argument('--niter', type=int, default=25, help='number of epochs to train for')28parser.add_argument('--lr', type=float, default=0.0002, help='learning rate, default=0.0002')29parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')30parser.add_argument('--cuda', action='store_true', help='enables cuda')31parser.add_argument('--ngpu', type=int, default=1, help='number of GPUs to use')32parser.add_argument('--netG', default='', help="path to netG (to continue training)")33parser.add_argument('--netD', default='', help="path to netD (to continue training)")34parser.add_argument('--outf', default='.', help='folder to output images and model checkpoints')35parser.add_argument('--manualSeed', type=int, help='manual seed')36parser.add_argument('--nBottleneck', type=int, default=4000, help='of dim for bottleneck of encoder')37parser.add_argument('--overlapPred', type=int, default=4, help='overlapping edges')38parser.add_argument('--nef', type=int, default=64, help='of encoder filters in first conv layer')39parser.add_argument('--wtl2', type=float, default=0.999, help='0 means do not use else use with this weight')40opt = parser.parse_args()41print(opt)42netG = _netG(opt)43# netG = TransformerNet()44netG.load_state_dict(torch.load(opt.netG, map_location=lambda storage, location: storage)['state_dict'])45# netG.requires_grad = False46netG.eval()47transform = transforms.Compose([transforms.ToTensor(),48                                transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])49image = utils.load_image(opt.test_image, opt.imageSize)50image = transform(image)51image = image.repeat(1, 1, 1, 1)52input_real = torch.FloatTensor(1, 3, opt.imageSize, opt.imageSize)53input_cropped = torch.FloatTensor(1, 3, opt.imageSize, opt.imageSize)54real_center = torch.FloatTensor(1, 3, opt.imageSize / 2, opt.imageSize / 2)55criterionMSE = nn.MSELoss()56# if opt.cuda:57#     netG.cuda()58#     input_real, input_cropped = input_real.cuda(),input_cropped.cuda()59#     criterionMSE.cuda()60#     real_center = real_center.cuda()61input_real = Variable(input_real)62input_cropped = Variable(input_cropped)63real_center = Variable(real_center)64input_real.data.resize_(image.size()).copy_(image)65input_cropped.data.resize_(image.size()).copy_(image)66real_center_cpu = image[:, :, opt.imageSize / 4:opt.imageSize / 4 +67                        opt.imageSize / 2, opt.imageSize / 4:opt.imageSize / 4 + opt.imageSize / 2]68real_center.data.resize_(real_center_cpu.size()).copy_(real_center_cpu)69input_cropped.data[:, 0, opt.imageSize / 4 + opt.overlapPred:opt.imageSize / 4 + opt.imageSize / 2 - opt.overlapPred,70                   opt.imageSize / 4 + opt.overlapPred:opt.imageSize / 4 + opt.imageSize / 2 - opt.overlapPred] = 2 * 117.0 / 255.0 - 1.071input_cropped.data[:, 1, opt.imageSize / 4 + opt.overlapPred:opt.imageSize / 4 + opt.imageSize / 2 - opt.overlapPred,72                   opt.imageSize / 4 + opt.overlapPred:opt.imageSize / 4 + opt.imageSize / 2 - opt.overlapPred] = 2 * 104.0 / 255.0 - 1.073input_cropped.data[:, 2, opt.imageSize / 4 + opt.overlapPred:opt.imageSize / 4 + opt.imageSize / 2 - opt.overlapPred,74                   opt.imageSize / 4 + opt.overlapPred:opt.imageSize / 4 + opt.imageSize / 2 - opt.overlapPred] = 2 * 123.0 / 255.0 - 1.075fake = netG(input_cropped)76errG = criterionMSE(fake, real_center)77recon_image = input_cropped.clone()78recon_image.data[:, :, opt.imageSize / 4:opt.imageSize / 4 + opt.imageSize /79                 2, opt.imageSize / 4:opt.imageSize / 4 + opt.imageSize / 2] = fake.data80utils.save_image('val_real_samples.png', image[0])81utils.save_image('val_cropped_samples.png', input_cropped.data[0])82utils.save_image('val_recon_samples.png', recon_image.data[0])...join_all_images.py
Source:join_all_images.py  
1from PIL import Image2def join_all_images():3    imp1 = Image.open("./Images/terms_wise_outstanding.png")4    widthx, heightx = imp1.size5    imp2 = Image.open("./Images/category_wise_credit.png")6    imageSize = Image.new('RGB', (1283, 481))7    imageSize.paste(imp1, (1, 0))8    imageSize.paste(imp2, (widthx + 2, 0))9    imageSize.save("./Images/all_credit.png")10    imp3 = Image.open("./Images/matured_credit.png")11    widthx, heightx = imp1.size12    imp4 = Image.open("./Images/aging_matured_credit.png")13    imageSize = Image.new('RGB', (1283, 481))14    imageSize.paste(imp3, (1, 0))15    imageSize.paste(imp4, (widthx + 2, 0))16    imageSize.save("./Images/all_Matured_credit.png")17    # ---------------------------------------------------------18    imp10 = Image.open("./Images/Category_wise_cash.png")19    widthx, heightx = imp1.size20    imp11 = Image.open("./Images/aging_cash_drop.png")21    imageSize = Image.new('RGB', (1283, 481))22    imageSize.paste(imp10, (1, 0))23    imageSize.paste(imp11, (widthx + 2, 0))24    imageSize.save("./Images/all_cash.png")25    # ------------- Closed to Matured, Matured Credit  ---------------------------26    imp5 = Image.open("./Images/regular_credit.png")27    widthx, heightx = imp1.size28    imp6 = Image.open("./Images/closed_to_matured_credit.png")29    imageSize = Image.new('RGB', (1283, 481))30    imageSize.paste(imp5, (1, 0))31    imageSize.paste(imp6, (widthx + 2, 0))32    imageSize.save("./Images/all_regular_credit.png")33    imp20 = Image.open("./Images/Cause_with_return.png")34    widthx, heightx = imp20.size35    imp21 = Image.open("./Images/LD_MTD_YTD_TARGET_vs_sales.png")36    imageSize = Image.new('RGB', (1283, 482))37    imageSize.paste(imp20, (1, 1))38    imageSize.paste(imp21, (widthx + 2, 1))39    imageSize.save("./Images/Cause_wise_delivery_man_wise_return.png")40    # ------------adding cause wise return and delivery man wise return----------41    imp22 = Image.open("./Images/Delivery_man_wise_return.png")42    widthx, heightx = imp22.size43    imageSize = Image.new('RGB', (1283, 482))44    imageSize.paste(imp22, (1, 1))45    imageSize.save("./Images/new_total_delivery_man_wise_return.png")46    # ------------adding cause wise return and delivery man wise return----------47    imp23 = Image.open("./Images/Day_Wise_Target_vs_Sales.png")48    widthx, heightx = imp23.size49    imageSize = Image.new('RGB', (1283, 482))50    imageSize.paste(imp23, (1, 1))51    imageSize.save("./Images//Day_wise_target_sales.png")52    imp24 = Image.open("./Images/Cumulative_Day_Wise_Target_vs_Sales.png")53    widthx, heightx = imp24.size54    imageSize = Image.new('RGB', (1283, 482))55    imageSize.paste(imp24, (1, 1))56    imageSize.save("./Images/Cumulative_Day_wise_target_sales.png")...weightMask.py
Source:weightMask.py  
1import torch2import torch.nn as nn3import torch.optim as optim4import torch.nn.functional as F5import collections6import numpy as np7from torch.autograd import Variable8## Netowrk Types ##9def generateSquareWeightMask(imageSize, boundarySize):10	##################################################################11	# Function GENERATE_SQUARE_WEIGHT_MASK12	# Takes in a nested dictionary of model/layer types13	# Outputs a nested dictionary of optimized hyperparameters14	# Parameters:15	# 		* imageSize: Size of one side of the image16	#       * boundarySize: Size of square grid to generate17	# Outputs18	#		* weightMask: Input for Repeated Layers Masked19	# 20	##################################################################21	numPixels = imageSize**222	weightMask = np.zeros((numPixels, numPixels))23	diagMask = np.zeros((numPixels, numPixels))24	for k in range(0, numPixels):25		i,j = ind2subImage(k, imageSize)26		pixelMask = np.zeros((imageSize, imageSize))27		if((i > 0) and (i < imageSize - 1) and (j > 0) and (j < imageSize - 1)):28			row_min = np.max((0, i - boundarySize))29			row_max = np.min((imageSize, i + boundarySize + 1))30			col_min = np.max((0, j - boundarySize))31			col_max = np.min((imageSize, j + boundarySize + 1))32			pixelMask[row_min:row_max, col_min:col_max] = 133		pixelMask[i,j] = 034		diagMask[k, k] = 135		weightMask[k, :] = np.reshape(pixelMask, (1, numPixels))36	weightMask = torch.from_numpy(weightMask).type(torch.cuda.BoolTensor)37	diagMask = torch.from_numpy(diagMask).type(torch.cuda.BoolTensor)38	return weightMask, diagMask39def generateGridWeightMask(imageSize):40	numPixels = imageSize**241	weightMask = np.zeros((numPixels, numPixels))42	diagMask = np.zeros((numPixels, numPixels))43	for k in range(0, numPixels):44		i,j = ind2subImage(k, imageSize)45		pixelMask = np.zeros((imageSize, imageSize))46		if((i > 0) and (i < imageSize - 1) and (j > 0) and (j < imageSize - 1)):47			#pixelMask[i, j] = 148			pixelMask[i - 1, j] = 149			pixelMask[i + 1, j] = 150			pixelMask[i, j + 1] = 151			pixelMask[i, j - 1] = 152		diagMask[k, k] = 153		# if (i > 0):54		# 	pixelMask[i - 1, j] = 155		# if (i < imageSize - 1):56		# 	pixelMask[i + 1, j] = 157		# if (j > 0):58		# 	pixelMask[i, j - 1] = 159		# if (j < imageSize - 1):60		# 	pixelMask[i, j + 1] = 161		weightMask[k, :] = np.reshape(pixelMask, (1, numPixels))62		#diagMask[k, k] = 163	weightMask = torch.from_numpy(weightMask).type(torch.cuda.BoolTensor)64	diagMask = torch.from_numpy(diagMask).type(torch.cuda.BoolTensor)65	return weightMask, diagMask66def ind2subImage(idx, imageSize):67	i = int(np.floor(idx/imageSize))68	j = idx % imageSize...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!!
