Best Python code snippet using uiautomator
ram512bit_test.py
Source:ram512bit_test.py
1import pytest2from ram512bit import RAM512Bit3def test_ram512bit():4 # MEMORY 05 test_input = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],6 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],7 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],9 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],10 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],11 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],12 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]13 test_address = [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 0],14 [0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 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 0, 0, 1, 1, 0],28 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]29 ram512Bit = RAM512Bit()30 for i in range(len(test_input)):31 result = ram512Bit.compute(test_input[i], test_address[i], test_load[i])32 assert result == test_output[i], 'Error, in: {}, address: {}, load: {} should output: {} but got: {}'.format(33 test_input[i],34 test_address[i],35 test_load[i],36 test_output[i],37 result)38 # MEMORY 739 test_input = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],40 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],41 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],42 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],43 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],44 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],45 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],46 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 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 0, 1, 0],60 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],61 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],62 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]63 for i in range(len(test_input)):64 result = ram512Bit.compute(test_input[i], test_address[i], test_load[i])65 assert result == test_output[i], 'Error, in: {}, address: {}, load: {} should output: {} but got: {}'.format(66 test_input[i],67 test_address[i],68 test_load[i],69 test_output[i],70 result)71 # MEMORY 1672 test_input = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],73 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],74 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],75 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],76 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],77 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],92 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],93 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],94 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],95 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]96 for i in range(len(test_input)):97 result = ram512Bit.compute(test_input[i], test_address[i], test_load[i])98 assert result == test_output[i], 'Error, in: {}, address: {}, load: {} should output: {} but got: {}'.format(99 test_input[i],100 test_address[i],101 test_load[i],102 test_output[i],103 result)104 # MEMORY 32105 test_input = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],106 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],107 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],108 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],109 [1, 1, 1, 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 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],123 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],124 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],125 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],126 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],127 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],128 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]129 for i in range(len(test_input)):130 result = ram512Bit.compute(test_input[i], test_address[i], test_load[i])131 assert result == test_output[i], 'Error, in: {}, address: {}, load: {} should output: {} but got: {}'.format(132 test_input[i],133 test_address[i],134 test_load[i],135 test_output[i],136 result)137 # MEMORY 64138 test_input = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],139 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],140 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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1, 1, 1, 1,153 0, 0, 0, 0, 0, 0, 0, 0]154 test_output = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],155 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],156 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],157 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],158 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],159 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],160 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],161 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]162 for i in range(len(test_input)):163 result = ram512Bit.compute(test_input[i], test_address[i], test_load[i])164 assert result == test_output[i], 'Error, in: {}, address: {}, load: {} should output: {} but got: {}'.format(165 test_input[i],166 test_address[i],167 test_load[i],168 test_output[i],...
ram64bit_test.py
Source:ram64bit_test.py
1import pytest2from ram64bit import RAM64Bit3def test_ram64bit():4 # MEMORY 05 test_input = [[0, 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16 [0, 0, 0, 1, 0, 1], [0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 1, 1]]17 test_load = [1, 1, 1, 1, 1, 1, 1, 1,18 0, 0, 0, 0, 0, 0, 0, 0]19 test_output = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],20 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],21 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],22 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],23 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],24 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],25 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],26 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]27 ram64Bit = RAM64Bit()28 for i in range(len(test_input)):29 result = ram64Bit.compute(test_input[i], test_address[i], test_load[i])30 assert result == test_output[31 i], 'Error, in[{}], address[{}], load[{}] should output: [{}] but got: [{}]'.format(test_input[i],32 test_address[i],33 test_load[i],34 test_output[i],35 result)36 # MEMORY 137 test_input = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],38 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],39 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],40 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],41 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],42 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],43 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],44 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]45 test_address = [[0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 1], [0, 0, 1, 0, 1, 0], [0, 0, 1, 0, 1, 1], [0, 0, 1, 1, 0, 0],46 [0, 0, 1, 1, 0, 1], [0, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1],47 [0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 1], [0, 0, 1, 0, 1, 0], [0, 0, 1, 0, 1, 1], [0, 0, 1, 1, 0, 0],48 [0, 0, 1, 1, 0, 1], [0, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1]]49 test_load = [1, 1, 1, 1, 1, 1, 1, 1,50 0, 0, 0, 0, 0, 0, 0, 0]51 test_output = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],52 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],53 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],54 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],55 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],56 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],57 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],58 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]59 for i in range(len(test_input)):60 result = ram64Bit.compute(test_input[i], test_address[i], test_load[i])61 assert result == test_output[62 i], 'Error, in[{}], address[{}], load[{}] should output: [{}] but got: [{}]'.format(test_input[i],63 test_address[i],64 test_load[i],65 test_output[i],66 result)67 # MEMORY 268 test_input = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],69 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],70 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],71 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],72 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],73 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],74 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],75 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]76 test_address = [[0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 1], [0, 1, 0, 0, 1, 0], [0, 1, 0, 0, 1, 1], [0, 1, 0, 1, 0, 0],77 [0, 1, 0, 1, 0, 1], [0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 1],78 [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 1], [0, 1, 0, 0, 1, 0], [0, 1, 0, 0, 1, 1], [0, 1, 0, 1, 0, 0],79 [0, 1, 0, 1, 0, 1], [0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 1]]80 test_load = [1, 1, 1, 1, 1, 1, 1, 1,81 0, 0, 0, 0, 0, 0, 0, 0]82 test_output = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],83 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],84 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],85 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],86 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],87 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],88 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],89 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]90 for i in range(len(test_input)):91 result = ram64Bit.compute(test_input[i], test_address[i], test_load[i])92 assert result == test_output[93 i], 'Error, in[{}], address[{}], load[{}] should output: [{}] but got: [{}]'.format(test_input[i],94 test_address[i],95 test_load[i],96 test_output[i],97 result)98 # MEMORY 799 test_input = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],100 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],101 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],102 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],103 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],104 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],105 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],106 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]107 test_address = [[1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 1], [1, 1, 1, 0, 1, 0], [1, 1, 1, 0, 1, 1], [1, 1, 1, 1, 0, 0],108 [1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1],109 [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 1], [1, 1, 1, 0, 1, 0], [1, 1, 1, 0, 1, 1], [1, 1, 1, 1, 0, 0],110 [1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1]]111 test_load = [1, 1, 1, 1, 1, 1, 1, 1,112 0, 0, 0, 0, 0, 0, 0, 0]113 test_output = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],114 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],115 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],116 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],117 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],118 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],119 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],120 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]121 for i in range(len(test_input)):122 result = ram64Bit.compute(test_input[i], test_address[i], test_load[i])123 assert result == test_output[124 i], 'Error, in[{}], address[{}], load[{}] should output: [{}] but got: [{}]'.format(test_input[i],125 test_address[i],126 test_load[i],127 test_output[i],...
main.py
Source:main.py
1import torch2import numpy as np3import dataset4import model5import training6import time7from pathlib import Path8from types import SimpleNamespace9import dnnutil10class AllPairContrastLoss(torch.nn.Module):11 def __init__(self, m=1.0):12 # we want the margin to be somewhat small. We need the contrast loss13 # in order to avoid mode collapse, but we care most about pushing 14 # same-class representations close together15 super(AllPairContrastLoss, self).__init__()16 self.m = m17 18 def forward(self, x, labels, parts):19 pairwise_dist_sq = torch.mm(x, x.t())20 squared_norm = pairwise_dist_sq.diag()21 pairwise_dist_sq = (22 squared_norm.view(1, -1) + 23 squared_norm.view(-1, 1) - 24 2 * pairwise_dist_sq)25 pairwise_dist_sq.clamp_(min=0.0)26 same_label = labels.view(-1, 1).eq(labels)27 same_part = parts.view(-1, 1).eq(parts)28 ## only taking top half of matrix, so not duplicating every element in symmetrical matrix29 same_label_flat = same_label[torch.triu(torch.ones(x.shape[0],x.shape[0])).eq(1)] 30 same_part_flat = same_part[torch.triu(torch.ones(x.shape[0],x.shape[0])).eq(1)] 31 32 pairwise_dist_sq_flat = pairwise_dist_sq[torch.triu(torch.ones(x.shape[0],x.shape[0])).eq(1)]33 pairs_same = pairwise_dist_sq_flat[same_label_flat * same_part_flat]34 pairs_diff = pairwise_dist_sq_flat[(1-same_label_flat)*same_part_flat]35 n_same = pairs_same.size(0)36 perm = torch.randperm(pairs_diff.size(0))37 idx = perm[:n_same]38 pairs_diff_new = pairs_diff[idx]39 loss_pos = pairs_same.mean()40 loss_neg = torch.clamp(self.m - pairs_diff_new.sqrt(), min=0).pow(2).mean()41 loss = loss_pos + loss_neg42 return loss43def accuracy(prediction, label):44 acc = torch.mean(torch.eq(prediction.argmax(1), label).float()).item()45 return acc46def setup_vanilla_wings(args):47 root = '/multiview/datasets/papillon/part_crops_new'48 train_data = dataset.WingDataset(root, train=True)49 test_data = dataset.WingDataset(root, train=False)50 kwargs = {51 'batch_size': args.batch_size,52 'num_workers': min(16, args.batch_size // 2),53 }54 DataLoader = torch.utils.data.DataLoader55 train_load = DataLoader(train_data, shuffle=True, **kwargs)56 test_load = DataLoader(test_data, shuffle=False, **kwargs)57 net = dnnutil.load_model(model.BFResnet, args.model)58 loss_fn = torch.nn.CrossEntropyLoss()59 opt = torch.optim.Adam(net.parameters(), lr=args.lr, weight_decay=1e-4)60 trainer = dnnutil.ClassifierTrainer(net, opt, loss_fn, accuracy)61 state = SimpleNamespace(62 train_load=train_load,63 test_load=test_load,64 net=net,65 loss_fn=loss_fn,66 opt=opt,67 trainer=trainer)68 return state69def setup_wingnet(args):70 #root = '/multiview/datasets/papillon/part_crops_gen'71 root = '/multiview/datasets/papillon/part_crops_new'72 largs = dict(73 batch_size=args.batch_size,74 num_workers=min(24, args.batch_size // 2),75 )76 kwargs = dict()77 invariant = args.invariant78 if invariant:79 kwargs.update(num_trans=args.num_trans )##return part ids with class labels and images80 dataset_class = dataset.InvariantWingDataset81 largs.update(collate_fn=dataset.multi_collate)82 else:83 kwargs.update(parts=True)84 dataset_class = dataset.WingDataset85 train_data = dataset_class(root, True, **kwargs)86 test_data = dataset_class(root, False, **kwargs)87 n = len(train_data)88 m = len(test_data)89 tr_samp = dataset.RandomSubsetSampler(n, n // 6)90 te_samp = dataset.RandomSubsetSampler(m, m // 6)91 DataLoader = torch.utils.data.DataLoader92 train_load = DataLoader(train_data, sampler=tr_samp, **largs)93 test_load = DataLoader(test_data, sampler=te_samp, **largs)94 nargs = dict(branches=4, invar=invariant, model='resnet18')95 if args.init_pretrained:96 net = dnnutil.load_model(model.Wingnet, args.init_pretrained, **nargs)97 net.resnet = dnnutil.load_model(model.ResnetBase, args.init_pretrained)98 else:99 net = dnnutil.network.load_model(model.Wingnet, args.init_pretrained, **nargs)100 if args.freeze:101 try:102 net.freeze_base_()103 params = net.head.parameters()104 except AttributeError:105 net.module.freeze_base_()106 params = net.module.head.parameters()107 else:108 params = net.parameters()109 loss = torch.nn.CrossEntropyLoss()110 siam_loss = AllPairContrastLoss()111 opt = torch.optim.Adam(params, lr=args.lr, weight_decay=1e-4)112 113 trainer = training.WingnetInvariantTrainer(114 net, opt, loss, siam_loss, accuracy, invariant, alpha=args.alpha)115 state = SimpleNamespace(116 train_load=train_load,117 test_load=test_load,118 net=net,119 opt=opt,120 trainer=trainer)121 return state122def setup_multiwing_weighting(args, use50=False):123 root = '/multiview/datasets/papillon/part_crops_new'124 train_data = dataset.SegStatsAndWingsDataset(root, train=True)125 test_data = dataset.SegStatsAndWingsDataset(root, train=False)126 collate = dataset.seg_multiwing_collate127 kwargs = {128 'batch_size': args.batch_size,129 'num_workers': 16,130 'collate_fn': collate,131 }132 DataLoader = torch.utils.data.DataLoader133 train_load = DataLoader(train_data, shuffle=True, **kwargs)134 test_load = DataLoader(test_data, shuffle=False, **kwargs)135 136 special = args.special ## special modification, use resnet base on each wing, no invariant layers137# import pdb; pdb.set_trace()138 if args.init_pretrained:139 net = dnnutil.network.load_model(model.WingnetA, args.init_pretrained, special=special)140 if special:141 net.basenet = dnnutil.network.load_model(model.BFResnet, args.init_pretrained)142 else:143 net.basenet = dnnutil.network.load_model(model.Wingnet,144 args.init_pretrained, branches=4)145 else:146 net = dnnutil.network.load_model(model.WingnetA, args.model)147 params = net.parameters()148 opt = torch.optim.Adam(params, lr=args.lr, weight_decay=1e-4)149 loss_fn = torch.nn.CrossEntropyLoss()150 trainer = training.SegMultiwingTrainer(net, opt, loss_fn, accuracy, special=special)151 state = SimpleNamespace(152 train_load=train_load,153 test_load=test_load,154 net=net,155 loss_fn=loss_fn,156 opt=opt,157 trainer=trainer)158 return state159def get_setup(args):160 method = args.method161 if method == 'base':162 state = setup_vanilla_wings(args)163 elif method == 'invar':164 state = setup_wingnet(args )165 elif method == 'multi':166 state = setup_multiwing_weighting(args)167 else:168 raise NotImplementedError(f'Method {method} is not yet supported')169 return state170def main():171 parser = dnnutil.basic_parser(lr=1e-4, batch_size=24)172 parser.add_argument('method', choices=['base', 'invar', 'multi',],173 help='Which stage to train')174 parser.add_argument('--alpha', type=float, default=0.01,175 help='Loss tradeoff (for invar model)')176 parser.add_argument('--init-pretrained', metavar='WEIGHTS', default='',177 help='Path to the weights of a model from which the model being '178 'trained is derived')179 parser.add_argument('--num-trans', type=int, default=4,180 help='Number of transforms to perform for the transform invariant layer')181 parser.add_argument('--freeze', action='store_true',182 help='Freeze the weights of the base network')183 parser.add_argument('--invariant', action='store_true',184 help='Train Invariant network with contrast loss')185 parser.add_argument('--drop', action='store_true',186 help='Drop Learning rate by one order of magnitude after 25 epochs')187 parser.add_argument('--special', action='store_true',188 help='Use resnet, without wing branches, best performance')189 parser.add_argument("--cmatrix", action='store_true',190 help="Print out confusion matrix")191 args = parser.parse_args()192 manager = dnnutil.Manager('runs', args.rid)193 manager.set_description(args.note)194 args = manager.load_state(args, restore_lr=False)195 state = get_setup(args)196 train_load, test_load = state.train_load, state.test_load197 net = state.net198 optim = state.opt199 trainer = state.trainer200 logger = dnnutil.TextLog(manager.run_dir / 'log.txt')201 checkpointer = dnnutil.Checkpointer(manager.run_dir, save_multi=True, period=5)202 logger.text(str(args))203 with manager.run_dir.joinpath('.lr').open('w') as f:204 f.write(str(args.lr))205 206 if args.method == 'multi':207 n_classes = 150208 else:209 n_classes = 0210 for e in range(args.start, args.start + args.epochs):211 212 with manager.run_dir.joinpath('.lr').open('r') as f:213 lr = float(f.read().strip())214 if args.drop and e >= 25:215 optim.param_groups[-1]['lr'] = lr*.1216 else:217 optim.param_groups[-1]['lr'] = lr218 219 start = time.time()220 if n_classes != 0:221 train_loss, train_acc, train_class_acc = trainer.train(train_load, e, nclasses = n_classes)222 test_loss, test_acc, test_class_acc = trainer.eval(test_load, e,nclasses = n_classes)223 if args.cmatrix:224 np.savez("cmatrix",test = test_class_acc, train=train_class_acc) 225 else:226 train_loss, train_acc = trainer.train(train_load, e)227 test_loss, test_acc = trainer.eval(test_load, e)228 t = time.time() - start229 lr = optim.param_groups[-1]['lr']230 logger.log(e, t, train_loss, train_acc, test_loss, test_acc, lr)231 checkpointer.checkpoint(net, test_loss, e)232if __name__ == '__main__':...
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!!