Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Custom Models, Layers, and Loss Functions with TensorFlow/Week 4 - Custom Models/utils.py
Views: 13373
import tensorflow as tf1from tensorflow.keras import layers23def test_loop(test_cases):45success = 06fails = 078for test_case in test_cases:9try:10assert test_case["result"] == test_case["expected"]11success += 11213except:14fails += 115print(f'{test_case["name"]}: {test_case["error_message"]}\nExpected: {test_case["expected"]}\nResult: {test_case["result"]}\n')1617if fails == 0:18print("\033[92m All public tests passed")1920else:21print('\033[92m', success," Tests passed")22print('\033[91m', fails, " Tests failed")23raise Exception(test_case["error_message"])242526def test_block_class(Block):2728filters = 6429kernel_size = 330padding = 'same'31pool_size = 332repetitions = 233test_block = Block(filters, kernel_size, repetitions, pool_size)34test_block(tf.random.uniform(shape=[2, 3, 4, 5]))3536vars_test_block = vars(test_block)3738test_cases = [39{40"name": "max_pool_type_check",41"result": type(test_block.max_pool),42"expected": layers.MaxPooling2D,43"error_message": f'Incorrect layer type for self.maxpool'44},45{46"name": "max_pool_size_check",47"result": vars_test_block['max_pool'].pool_size,48"expected": (pool_size, pool_size),49"error_message": f'max pool size incorrect. check parameters.'50},51{52"name": "max_pool_size_check",53"result": vars_test_block['max_pool'].strides,54"expected": (2,2),55"error_message": f'max pool strides incorrect. check parameters.'56},57{58"name": "conv2D_0_type_check",59"result": type(vars_test_block['conv2D_0']),60"expected": layers.Conv2D,61"error_message": f'Incorrect layer type for block_0'62},63{64"name": "conv2D_1_type_check",65"result": type(vars_test_block['conv2D_1']),66"expected": layers.Conv2D,67"error_message": f'Incorrect layer type for block_0'68},69{70"name": "conv2D_0_filters_check",71"result": vars_test_block['conv2D_0'].filters,72"expected": filters,73"error_message": f'Incorrect filters for Conv2D layer. Please check parameters.'74},75{76"name": "conv2D_0_kernel_size_check",77"result": vars_test_block['conv2D_0'].kernel_size,78"expected": (kernel_size, kernel_size),79"error_message": f'Incorrect kernel_size for Conv2D layer. Please check parameters.'80},81{82"name": "conv2D_0_activation_check",83"result": vars_test_block['conv2D_0'].activation,84"expected": tf.keras.activations.relu,85"error_message": f'Incorrect activation for Conv2D layer. Please check parameters.'86},87{88"name": "conv2D_0_padding_check",89"result": vars_test_block['conv2D_0'].padding,90"expected": padding,91"error_message": f'Incorrect padding for Conv2D layer. Please check parameters.'92},9394]9596test_loop(test_cases)9798def test_myvgg_class(MyVGG, Block):99test_vgg = MyVGG(num_classes=2)100test_vgg_layers = test_vgg.layers101102def get_block_params(block):103return (block.filters, block.kernel_size, block.repetitions)104105test_cases = [106{107"name": "block_a_type_check",108"result": type(test_vgg.block_a),109"expected": Block,110"error_message": "self.block_a has an incorrect type. Please check declaration."111},112{113"name": "block_b_type_check",114"result": type(test_vgg.block_b),115"expected": Block,116"error_message": "self.block_b has an incorrect type. Please check declaration."117},118{119"name": "block_c_type_check",120"result": type(test_vgg.block_c),121"expected": Block,122"error_message": "self.block_c has an incorrect type. Please check declaration."123},124{125"name": "block_d_type_check",126"result": type(test_vgg.block_d),127"expected": Block,128"error_message": "self.block_d has an incorrect type. Please check declaration."129},130{131"name": "block_e_type_check",132"result": type(test_vgg.block_e),133"expected": Block,134"error_message": "self.block_e has an incorrect type. Please check declaration."135},136{137"name": "block_a_param_check",138"result": get_block_params(test_vgg.block_a),139"expected": (64, 3, 2),140"error_message": "self.block_a has incorrect parameters. Please check hints in the code comments."141},142{143"name": "block_b_param_check",144"result": get_block_params(test_vgg.block_b),145"expected": (128, 3, 2),146"error_message": "self.block_b has incorrect parameters. Please check hints in the code comments."147},148{149"name": "block_c_param_check",150"result": get_block_params(test_vgg.block_c),151"expected": (256, 3, 3),152"error_message": "self.block_c has incorrect parameters. Please check hints in the code comments."153},154{155"name": "block_d_param_check",156"result": get_block_params(test_vgg.block_d),157"expected": (512, 3, 3),158"error_message": "self.block_d has incorrect parameters. Please check hints in the code comments."159},160{161"name": "block_e_param_check",162"result": get_block_params(test_vgg.block_e),163"expected": (512, 3, 3),164"error_message": "self.block_e has incorrect parameters. Please check hints in the code comments."165},166{167"name": "flatten_type_check",168"result": type(test_vgg.flatten),169"expected": layers.Flatten,170"error_message": "self.flatten has an incorrect type. Please check declaration."171},172{173"name": "fc_type_check",174"result": type(test_vgg.fc),175"expected": layers.Dense,176"error_message": "self.fc has an incorrect type. Please check declaration."177},178{179"name": "fc_units_check",180"result": test_vgg.fc.units,181"expected": 256,182"error_message": "self.fc has an incorrect number of units. Please check declaration."183},184{185"name": "fc_activation_check",186"result": test_vgg.fc.activation,187"expected": tf.keras.activations.relu,188"error_message": "self.fc has an incorrect activation. Please check declaration."189},190{191"name": "classifier_type_check",192"result": type(test_vgg.classifier),193"expected": layers.Dense,194"error_message": "self.classifier has an incorrect type. Please check declaration."195},196{197"name": "fc_units_check",198"result": test_vgg.classifier.units,199"expected": 2,200"error_message": "self.classifier has an incorrect number of units. Please check declaration."201},202{203"name": "fc_activation_check",204"result": test_vgg.classifier.activation,205"expected": tf.keras.activations.softmax,206"error_message": "self.classifier has an incorrect activation. Please check declaration."207},208{209"name": "layer_0_check",210"result": type(test_vgg_layers[0]),211"expected": Block,212"error_message": "Layer 0 of myVGG is incorrect. Please check its call() method."213},214{215"name": "layer_1_check",216"result": type(test_vgg_layers[1]),217"expected": Block,218"error_message": "Layer 1 of myVGG is incorrect. Please check its call() method."219},220{221"name": "layer_2_check",222"result": type(test_vgg_layers[2]),223"expected": Block,224"error_message": "Layer 2 of myVGG is incorrect. Please check its call() method."225},226{227"name": "layer_3_check",228"result": type(test_vgg_layers[3]),229"expected": Block,230"error_message": "Layer 3 of myVGG is incorrect. Please check its call() method."231},232{233"name": "layer_4_check",234"result": type(test_vgg_layers[4]),235"expected": Block,236"error_message": "Layer 4 of myVGG is incorrect. Please check its call() method."237},238{239"name": "layer_5_check",240"result": type(test_vgg_layers[5]),241"expected": layers.Flatten,242"error_message": "Layer 5 of myVGG is incorrect. Please check its call() method."243},244{245"name": "layer_6_check",246"result": type(test_vgg_layers[6]),247"expected": layers.Dense,248"error_message": "Layer 6 of myVGG is incorrect. Please check its call() method."249},250{251"name": "layer_7_check",252"result": type(test_vgg_layers[7]),253"expected": layers.Dense,254"error_message": "Layer 7 of myVGG is incorrect. Please check its call() method."255},256257]258259test_loop(test_cases)260261262