def processImage(self, jpegbytes): self.currentImage = imresize( pygame.surfarray.array3d(pygame.image.load(cStringIO.StringIO(jpegbytes), 'tmp.jpg').convert()), (32, 24)) self.currentImage = ndi.gaussian_filter(self.currentImage, .5) - ndi.gaussian_filter(self.currentImage, 1) self.currentImage = self.currentImage / 255.0 self.pattern = np.tile(np.reshape(self.currentImage, (32 * 24 * 3)), (64, 1)) self.pattern = self.pattern + 0.001 * (1 - np.random.random((self.pattern.shape[0], self.pattern.shape[1]))) self.bias = np.ones((self.pattern.shape[0], 1)) self.pattern = np.concatenate((self.pattern, self.bias), axis=1) self.act1 = np.concatenate((np.squeeze(np.array(af(np.dot(self.pattern, self.w1)))), self.bias), axis=1) self.act2 = np.squeeze(np.array(af(np.dot(self.act1, self.w2)))) self.act22 = 0 * self.act2 for i in range(self.act2.shape[0]): self.act22[i, np.argmax(self.act2[i, :])] = 1 self.nn_treads = self.action_vectors_motor[np.argmax(np.sum(self.act22, axis=0))] print self.nn_treads if np.sum(np.abs(self.treads)): for i in range(np.asarray(self.action_vectors_motor).shape[0]): if self.action_vectors_motor[i] == self.treads: break self.category = np.tile(self.action_vectors_neuro[i], (self.pattern.shape[0], 1)) self.error = self.category - self.act2 self.sse = np.power(self.error, 2).sum self.delta_w2 = self.error * self.act2 * (1 - self.act2) self.delta_w1 = np.dot(self.delta_w2, self.w2.transpose()) * self.act1 * (1 - self.act1) self.delta_w1 = np.delete(self.delta_w1, -1, 1) self.dw1 = np.dot(self.L1, np.dot(self.pattern.transpose(), self.delta_w1)) + self.M * self.dw1 self.dw2 = np.dot(self.L2, np.dot(self.act1.transpose(), self.delta_w2)) + self.M * self.dw2 self.w1 = self.w1 + self.dw1 self.w2 = self.w2 + self.dw2 self.w1 = self.w1 + 0.00001 * (-0.5 + np.random.random((self.w1.shape[0], self.w1.shape[1]))) self.w2 = self.w2 + 0.00001 * (-0.5 + np.random.random((self.w2.shape[0], self.w2.shape[1])))
def forward_get_all(self, inputs): """ Returns the outputs from all layers, from input to output. Parameters ---------- inputs : NxD np array An array with N samples with D dimensions (features). For example an input with 2 samples and 3 dimensions: inputs = array([[1,2,3], [3,4,5]]) Returns ------- result : list A list of 2d np arrays. The all the arrays will have N rows, the number of outputs for each array will depend on the of nodes in that particular layer. """ w = self.weights # shorthand samples,d = np.shape(inputs) res = [] x = np.concatenate((inputs, np.array([np.ones(samples)]).T), axis=1) for i in range(0, self.get_num_layers()): af = self.act_funcs[i][0] y = af(np.dot(x, w[i].T)) res.append(y) if i+1 != self.get_num_layers(): x = np.concatenate((y, np.array([np.ones(samples)]).T), axis=1) return res
def processImage(self, jpegbytes): self.currentImage = imresize( pygame.surfarray.array3d( pygame.image.load(cStringIO.StringIO(jpegbytes), 'tmp.jpg').convert()), (32, 24)) self.currentImage = ndi.gaussian_filter( self.currentImage, .5) - ndi.gaussian_filter(self.currentImage, 1) self.currentImage = self.currentImage / 255.0 self.pattern = np.tile(np.reshape(self.currentImage, (32 * 24 * 3)), (64, 1)) self.pattern = self.pattern + 0.001 * (1 - np.random.random( (self.pattern.shape[0], self.pattern.shape[1]))) self.bias = np.ones((self.pattern.shape[0], 1)) self.pattern = np.concatenate((self.pattern, self.bias), axis=1) self.act1 = np.concatenate((np.squeeze( np.array(af(np.dot(self.pattern, self.w1)))), self.bias), axis=1) self.act2 = np.squeeze(np.array(af(np.dot(self.act1, self.w2)))) self.act22 = 0 * self.act2 for i in range(self.act2.shape[0]): self.act22[i, np.argmax(self.act2[i, :])] = 1 self.nn_treads = self.action_vectors_motor[np.argmax( np.sum(self.act22, axis=0))] print self.nn_treads if np.sum(np.abs(self.treads)): for i in range(np.asarray(self.action_vectors_motor).shape[0]): if self.action_vectors_motor[i] == self.treads: break self.category = np.tile(self.action_vectors_neuro[i], (self.pattern.shape[0], 1)) self.error = self.category - self.act2 self.sse = np.power(self.error, 2).sum self.delta_w2 = self.error * self.act2 * (1 - self.act2) self.delta_w1 = np.dot( self.delta_w2, self.w2.transpose()) * self.act1 * (1 - self.act1) self.delta_w1 = np.delete(self.delta_w1, -1, 1) self.dw1 = np.dot(self.L1, np.dot(self.pattern.transpose(), self.delta_w1)) + self.M * self.dw1 self.dw2 = np.dot(self.L2, np.dot(self.act1.transpose(), self.delta_w2)) + self.M * self.dw2 self.w1 = self.w1 + self.dw1 self.w2 = self.w2 + self.dw2 self.w1 = self.w1 + 0.00001 * (-0.5 + np.random.random( (self.w1.shape[0], self.w1.shape[1]))) self.w2 = self.w2 + 0.00001 * (-0.5 + np.random.random( (self.w2.shape[0], self.w2.shape[1])))
def process_image_from_rover(self, jpegbytes): try: self.currentImage = imresize( pygame.surfarray.array3d(pygame.image.load(cStringIO.StringIO(jpegbytes), "tmp.jpg").convert()), (32, 24), ) self.currentImage = ndi.gaussian_filter(self.currentImage, 0.5) - ndi.gaussian_filter(self.currentImage, 1) self.currentImage = self.currentImage / 255.0 self.pattern = np.tile(np.reshape(self.currentImage, (32 * 24 * 3)), (64, 1)) self.pattern += 0.001 * (1 - np.random.random((self.pattern.shape[0], self.pattern.shape[1]))) self.bias = np.ones((self.pattern.shape[0], 1)) self.pattern = np.concatenate((self.pattern, self.bias), axis=1) self.act1 = np.concatenate( (np.squeeze(np.array(af(np.dot(self.pattern, self.network_weight_one)))), self.bias), axis=1 ) self.act2 = np.squeeze(np.array(af(np.dot(self.act1, self.network_weight_two)))) self.act22 = 0 * self.act2 for i in range(self.act2.shape[0]): self.act22[i, np.argmax(self.act2[i, :])] = 1 self.nn_treads = self.action_vectors_motor[np.argmax(np.sum(self.act22, axis=0))] print(self.nn_treads) if np.sum(np.abs(self.treads)): for i in range(np.asarray(self.action_vectors_motor).shape[0]): if self.action_vectors_motor[i] == self.treads: break self.category = np.tile(self.action_vectors_neuro[i], (self.pattern.shape[0], 1)) self.error = self.category - self.act2 self.sse = np.power(self.error, 2).sum self.delta_w2 = self.error * self.act2 * (1 - self.act2) self.delta_w1 = np.dot(self.delta_w2, self.network_weight_two.transpose()) * self.act1 * (1 - self.act1) self.delta_w1 = np.delete(self.delta_w1, -1, 1) self.dw1 = ( np.dot(self.network_learning_rate_one, np.dot(self.pattern.transpose(), self.delta_w1)) + self.M * self.dw1 ) self.dw2 = ( np.dot(self.network_learning_rate_two, np.dot(self.act1.transpose(), self.delta_w2)) + self.M * self.dw2 ) self.network_weight_one = self.network_weight_one + self.dw1 self.network_weight_two = self.network_weight_two + self.dw2 self.network_weight_one += 0.0001 * ( -0.5 + np.random.random((self.network_weight_one.shape[0], self.network_weight_one.shape[1])) ) # increase random Value self.network_weight_two += 0.0001 * ( -0.5 + np.random.random((self.network_weight_two.shape[0], self.network_weight_two.shape[1])) ) # increase random Value except: pass