Best Python code snippet using localstack_python
train.py
Source: train.py
1if __name__ == "__main__":2 import argparse3 from src.IdentificationNetwork import IdentificationNetwork4 from src.TrackingNetwork import TrackingNetwork5 from src.Util import get_fixed_path6 import tensorflow.keras.backend as K7 import src.nets89 parser = argparse.ArgumentParser()1011 parser.add_argument("-i", "--datasets_folder", help="Location of the \"Datasets\" folder", type=str, required=True)12 parser.add_argument("-o", "--save_folder", help="Where to save the models", type=str, required=True)1314 parser.add_argument("-l", "--load_folder", help="If and from where to load the models' weights", type=str,15 required=False, default=None)1617 parser.add_argument("-pte", "--pre_tracking_epochs", help="Epochs for the pre tracking network", type=int,18 required=False, default=20)19 parser.add_argument("-te", "--tracking_epochs", help="Epochs for the tracking network", type=int,20 required=False, default=50)21 parser.add_argument("-ie", "--identification_epochs", help="Epochs for the identification network", type=int,22 required=False, default=25)2324 parser.add_argument("-spe", "--steps_per_epoch", help="Steps per epoch", type=int, required=False, default=128)25 parser.add_argument("-bs", "--batch_size", help="Batch size", type=int, required=False, default=2)2627 parser.add_argument("-dc", "--do_checkpoint", help="Should save checkpoints", type=bool, required=False, default=True)2829 args = parser.parse_args()3031 datasets_folder = get_fixed_path(args.datasets_folder, replace_backslash=True, add_backslash=True)32 save_folder = get_fixed_path(args.save_folder, replace_backslash=True, add_backslash=True)33 load_folder = get_fixed_path(args.load_folder, replace_backslash=True, add_backslash=True) if args.load_folder is not None else None3435 ####################################################################################################################36 # Pre-tracking network37 ####################################################################################################################38 if args.pre_tracking_epochs > 0:39 K.clear_session()4041 if load_folder is None:42 net_pre_tracking = IdentificationNetwork(src.nets.generate_pre_tracking_model(input_shape=(416, 416, 1), number_of_classes=7))4344 net_pre_tracking.train_model(epochs=args.pre_tracking_epochs,45 steps_per_epoch=args.steps_per_epoch,46 weights_save_path=save_folder + "pre_tracking.h5",47 weights_load_path=None,48 do_checkpoint=args.do_checkpoint,49 generator=IdentificationNetwork.generator(50 folder_images=datasets_folder + "PreTracking/",51 batch_size=args.batch_size,52 image_ext=(".jpg", ".png")53 ))5455 ####################################################################################################################56 # Tracking network57 ####################################################################################################################58 if args.tracking_epochs > 0:59 K.clear_session()6061 net_tracking = TrackingNetwork(src.nets.generate_tracking_model(input_shape=(416, 416, 1),62 output_shape=(13, 13, 11),63 number_of_classes=6))6465 load_path_tracking = load_folder + "tracking.h5" if load_folder is not None else save_folder + "pre_tracking.h5"6667 net_tracking.get_model().summary()6869 net_tracking.train_model(epochs=args.tracking_epochs,70 steps_per_epoch=args.steps_per_epoch,71 weights_save_path=save_folder + "tracking.h5",72 weights_load_path=load_path_tracking,73 do_checkpoint=args.do_checkpoint,74 generator=TrackingNetwork.generator(75 folder_images=datasets_folder + "Tracking/",76 folder_grids=datasets_folder + "Tracking/",77 batch_size=args.batch_size78 ))7980 ####################################################################################################################81 # Identification network82 ####################################################################################################################83 if args.identification_epochs > 0:84 K.clear_session()8586 net_identification = IdentificationNetwork(models=(87 src.nets.generate_identification_model(input_shape=(50, 50, 1), number_of_classes=9),88 src.nets.generate_identification_model(input_shape=(50, 50, 1), number_of_classes=4),89 src.nets.generate_identification_model(input_shape=(50, 50, 1), number_of_classes=2),90 src.nets.generate_identification_model(input_shape=(50, 50, 1), number_of_classes=2),91 src.nets.generate_identification_model(input_shape=(50, 50, 1), number_of_classes=4),92 src.nets.generate_identification_model(input_shape=(50, 50, 1), number_of_classes=2)93 ))9495 load_path_identification = load_folder + "identification.h5" if load_folder is not None else None9697 net_identification.train_models(number_of_models=6,98 epochs=args.identification_epochs,99 steps_per_epoch=args.steps_per_epoch,100 weights_save_path=save_folder + "identification.h5",101 weights_load_path=None,102 do_checkpoint=args.do_checkpoint,103 generators=IdentificationNetwork.generators(104 folder_images=datasets_folder + "Identification/",105 batch_size=args.batch_size,106 image_ext=(".jpg", ".png")
...
classifier.py
Source: classifier.py
1import torch2import torch.nn as nn3from torch.utils.checkpoint import checkpoint4from tortoise.models.arch_util import Upsample, Downsample, normalization, zero_module, AttentionBlock5class ResBlock(nn.Module):6 def __init__(7 self,8 channels,9 dropout,10 out_channels=None,11 use_conv=False,12 use_scale_shift_norm=False,13 dims=2,14 up=False,15 down=False,16 kernel_size=3,17 do_checkpoint=True,18 ):19 super().__init__()20 self.channels = channels21 self.dropout = dropout22 self.out_channels = out_channels or channels23 self.use_conv = use_conv24 self.use_scale_shift_norm = use_scale_shift_norm25 self.do_checkpoint = do_checkpoint26 padding = 1 if kernel_size == 3 else 227 self.in_layers = nn.Sequential(28 normalization(channels),29 nn.SiLU(),30 nn.Conv1d(channels, self.out_channels, kernel_size, padding=padding),31 )32 self.updown = up or down33 if up:34 self.h_upd = Upsample(channels, False, dims)35 self.x_upd = Upsample(channels, False, dims)36 elif down:37 self.h_upd = Downsample(channels, False, dims)38 self.x_upd = Downsample(channels, False, dims)39 else:40 self.h_upd = self.x_upd = nn.Identity()41 self.out_layers = nn.Sequential(42 normalization(self.out_channels),43 nn.SiLU(),44 nn.Dropout(p=dropout),45 zero_module(46 nn.Conv1d(self.out_channels, self.out_channels, kernel_size, padding=padding)47 ),48 )49 if self.out_channels == channels:50 self.skip_connection = nn.Identity()51 elif use_conv:52 self.skip_connection = nn.Conv1d(53 dims, channels, self.out_channels, kernel_size, padding=padding54 )55 else:56 self.skip_connection = nn.Conv1d(dims, channels, self.out_channels, 1)57 def forward(self, x):58 if self.do_checkpoint:59 return checkpoint(60 self._forward, x61 )62 else:63 return self._forward(x)64 def _forward(self, x):65 if self.updown:66 in_rest, in_conv = self.in_layers[:-1], self.in_layers[-1]67 h = in_rest(x)68 h = self.h_upd(h)69 x = self.x_upd(x)70 h = in_conv(h)71 else:72 h = self.in_layers(x)73 h = self.out_layers(h)74 return self.skip_connection(x) + h75class AudioMiniEncoder(nn.Module):76 def __init__(self,77 spec_dim,78 embedding_dim,79 base_channels=128,80 depth=2,81 resnet_blocks=2,82 attn_blocks=4,83 num_attn_heads=4,84 dropout=0,85 downsample_factor=2,86 kernel_size=3):87 super().__init__()88 self.init = nn.Sequential(89 nn.Conv1d(spec_dim, base_channels, 3, padding=1)90 )91 ch = base_channels92 res = []93 self.layers = depth94 for l in range(depth):95 for r in range(resnet_blocks):96 res.append(ResBlock(ch, dropout, do_checkpoint=False, kernel_size=kernel_size))97 res.append(Downsample(ch, use_conv=True, out_channels=ch*2, factor=downsample_factor))98 ch *= 299 self.res = nn.Sequential(*res)100 self.final = nn.Sequential(101 normalization(ch),102 nn.SiLU(),103 nn.Conv1d(ch, embedding_dim, 1)104 )105 attn = []106 for a in range(attn_blocks):107 attn.append(AttentionBlock(embedding_dim, num_attn_heads, do_checkpoint=False))108 self.attn = nn.Sequential(*attn)109 self.dim = embedding_dim110 def forward(self, x):111 h = self.init(x)112 h = self.res(h)113 h = self.final(h)114 for blk in self.attn:115 h = checkpoint(blk, h)116 return h[:, :, 0]117class AudioMiniEncoderWithClassifierHead(nn.Module):118 def __init__(self, classes, distribute_zero_label=True, **kwargs):119 super().__init__()120 self.enc = AudioMiniEncoder(**kwargs)121 self.head = nn.Linear(self.enc.dim, classes)122 self.num_classes = classes123 self.distribute_zero_label = distribute_zero_label124 def forward(self, x, labels=None):125 h = self.enc(x)126 logits = self.head(h)127 if labels is None:128 return logits129 else:130 if self.distribute_zero_label:131 oh_labels = nn.functional.one_hot(labels, num_classes=self.num_classes)132 zeros_indices = (labels == 0).unsqueeze(-1)133 # Distribute 20% of the probability mass on all classes when zero is specified, to compensate for dataset noise.134 zero_extra_mass = torch.full_like(oh_labels, dtype=torch.float, fill_value=.2/(self.num_classes-1))135 zero_extra_mass[:, 0] = -.2136 zero_extra_mass = zero_extra_mass * zeros_indices137 oh_labels = oh_labels + zero_extra_mass138 else:139 oh_labels = labels140 loss = nn.functional.cross_entropy(logits, oh_labels)...
Check out the latest blogs from LambdaTest on this topic:
The fact is not alien to us anymore that cross browser testing is imperative to enhance your application’s user experience. Enhanced knowledge of popular and highly acclaimed testing frameworks goes a long way in developing a new app. It holds more significance if you are a full-stack developer or expert programmer.
QA testers have a unique role and responsibility to serve the customer. Serving the customer in software testing means protecting customers from application defects, failures, and perceived failures from missing or misunderstood requirements. Testing for known requirements based on documentation or discussion is the core of the testing profession. One unique way QA testers can both differentiate themselves and be innovative occurs when senseshaping is used to improve the application user experience.
Having a good web design can empower business and make your brand stand out. According to a survey by Top Design Firms, 50% of users believe that website design is crucial to an organization’s overall brand. Therefore, businesses should prioritize website design to meet customer expectations and build their brand identity. Your website is the face of your business, so it’s important that it’s updated regularly as per the current web design trends.
Enterprise resource planning (ERP) is a form of business process management software—typically a suite of integrated applications—that assists a company in managing its operations, interpreting data, and automating various back-office processes. The introduction of a new ERP system is analogous to the introduction of a new product into the market. If the product is not handled appropriately, it will fail, resulting in significant losses for the business. Most significantly, the employees’ time, effort, and morale would suffer as a result of the procedure.
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!!