def sample(self, parameters, char_to_ix, sample_limit=50): vocab_size, n_a = self.n_x, self.n_a Wx = parameters['Wx'] Wa = parameters['Wa'] Wy = parameters['Wy'] b = parameters['b'] by = parameters['by'] x = np.zeros((vocab_size, 1)) a_prev = np.zeros((n_a, 1)) idx = -1 indices = [] counter = 0 newline_idx = char_to_ix['\n'] while idx != newline_idx and counter != sample_limit: a = np.tanh(np.dot(Wx, x) + np.dot(Wa, a_prev) + b) y = softmax(np.dot(Wy, a) + by) idx = np.random.choice(list(range(vocab_size)), p=y.ravel()) indices.append(idx) x = np.zeros((vocab_size, 1)) x[idx] = 1 a_prev = a counter += 1 if counter == 50: indices.append(newline_idx) return indices
def step(self, inputs, states): vP_t = inputs hP_tm1 = states[0] _ = states[1:3] # ignore internal dropout/masks vP, WP_v, WPP_v, v, W_g2 = states[3:8] vP_mask, = states[8:] WP_v_Dot = K.dot(vP, WP_v) WPP_v_Dot = K.dot(K.expand_dims(vP_t, axis=1), WPP_v) s_t_hat = K.tanh(WPP_v_Dot + WP_v_Dot) s_t = K.dot(s_t_hat, v) s_t = K.batch_flatten(s_t) a_t = softmax(s_t, mask=vP_mask, axis=1) c_t = K.batch_dot(a_t, vP, axes=[1, 1]) GRU_inputs = K.concatenate([vP_t, c_t]) g = K.dot(GRU_inputs, W_g2) GRU_inputs = g * GRU_inputs hP_t, s = super(SelfAttnGRU, self).step(GRU_inputs, states) return hP_t, s
def predict(self, molecules, return_prob=True): molecules = deepcopy(molecules) # Binarize input if self.binary: molecules = self.binarize(molecules) predictions = [] probabilities = [] for molecule in molecules: P = [0] * self.num_classes for c in self.classes: # add log prior P[c] = np.log(self.P_c[c]) for atom in molecule: if atom >= 0: # calculate log likelihood. Molecules has pad as index 0, so shift it one P[c] += np.log(self.p_w_given_c[int(atom), c]) # normalize the posterior distribution, using softmax P = softmax(P) predictions.append(np.argmax(P)) probabilities.append(P) if return_prob: return predictions, probabilities return predictions
def run_inference(inf_file): # Preprocessing of the image happens here img = load_image(inf_file) print("Image Loaded") img = make_square(img) img = augment(prep, img) print("Transformations done") img = img.transpose(-1, 0, 1).astype(np.float32) img = img.reshape(-1, CHANNELS, IMG_WIDTH, IMG_HEIGHT) # Inferencing starts here sess = onnxruntime.InferenceSession("./best_acc.onnx") print("The model expects input shape: ", sess.get_inputs()[0].shape) print("The shape of the Image is: ", img.shape) input_name = sess.get_inputs()[0].name result = sess.run(None, {input_name: img}) prob_array = result[0][0] print("Prob Array ", prob_array) prob = max(softmax(result[0][0])) print("Prob ", prob) species = tf.argmax(prob_array.ravel()[:10]).numpy() print("Class Label ", species) print("Spec ", CLASS_MAP[species][1]) string_label = CLASS_MAP[species][1].split(" ") return (string_label[0], string_label[1], str(prob), color_code(prob))
def feedForward(self, inputRow): hiddenNet = (self.hiddenLayerWeights @ inputRow) + self.hiddenLayerBiases hiddenActivation = self.activationFun(hiddenNet) outputNet = (self.outputLayerWeights @ hiddenActivation) + self.outputLayerBiases outputActivation = helpers.softmax(outputNet) self.hiddenLayerNetValues = hiddenNet self.hiddenLayerActivations.append(hiddenActivation) self.outputLayerNetValues = outputNet self.outputLayerActivations.append(outputActivation) if self.useNag: hiddenNagNet = ((self.hiddenLayerWeights - self.hiddenMomentum) @ inputRow) + (self.hiddenLayerBiases - self.hiddenBiasMomentum) outputNagNet = ((self.outputLayerWeights - self.outputMomentum) @ hiddenActivation) + (self.outputLayerBiases - self.outputBiasMomentum) outputNagActivation = helpers.softmax(outputNagNet) self.hiddenNagNetValues = hiddenNagNet self.outputNagActivation = outputNagActivation return outputActivation
def forward(self, xt, a_prev, parameters): Wx = parameters['Wx'] Wa = parameters['Wa'] Wy = parameters['Wy'] b = parameters['b'] by = parameters['by'] a_next = np.tanh(np.dot(Wx, xt) + np.dot(Wa, a_prev) + b) yt = softmax(np.dot(Wy, a_next) + by) cache = {'a_prev': a_prev, 'a_next': a_next, 'xt': xt, 'yt': yt} return a_next, yt
def call(self, inputs, mask=None): assert (isinstance(inputs, list) and len(inputs) == 5) uQ, WQ_u, WQ_v, v, VQ_r = inputs uQ_mask = mask[0] if mask is not None else None ones = K.ones_like(K.sum(uQ, axis=1, keepdims=True)) # (B, 1, 2H) s_hat = K.dot(uQ, WQ_u) s_hat += K.dot(ones, K.dot(WQ_v, VQ_r)) s_hat = K.tanh(s_hat) s = K.dot(s_hat, v) s = K.batch_flatten(s) a = softmax(s, mask=uQ_mask, axis=1) rQ = K.batch_dot(uQ, a, axes=[1, 1]) return rQ
def forward_pass(self, inp): for ix in range(len(inp)): if inp[ix] == 0: self.activation[0][ix] = 1 else: self.activation[0][ix] = 0 self.X = self.activation self.L1 = np.tanh(np.dot(self.X, self.W1)) self.L2 = np.tanh(np.dot(self.L1, self.W2)) self.Y = softmax(np.dot(self.L2, self.W3)) self.y_out = [] for ix in range(self.Y.shape[1]): self.y_out.append(self.Y[0][ix] * self.activation[0][ix]) self.OUT = np.argmax(self.y_out) return self.OUT
def step(self, inputs, states): # input ha_tm1 = states[0] # (B, 2H) _ = states[1:3] # ignore internal dropout/masks hP, WP_h, Wa_h, v = states[3:7] # (B, P, 2H) hP_mask, = states[7:8] WP_h_Dot = K.dot(hP, WP_h) # (B, P, H) Wa_h_Dot = K.dot(K.expand_dims(ha_tm1, axis=1), Wa_h) # (B, 1, H) s_t_hat = K.tanh(WP_h_Dot + Wa_h_Dot) # (B, P, H) s_t = K.dot(s_t_hat, v) # (B, P, 1) s_t = K.batch_flatten(s_t) # (B, P) a_t = softmax(s_t, mask=hP_mask, axis=1) # (B, P) c_t = K.batch_dot(hP, a_t, axes=[1, 1]) # (B, 2H) GRU_inputs = c_t ha_t, (ha_t_, ) = super(PointerGRU, self).step(GRU_inputs, states) return a_t, [ha_t]
def run_inference(inf_file): # Preprocessing of the image happens here img = load_image(inf_file) originalimg = img #Cropping line is below, SHOULD NOT BE INCLUDED IN THIS VERSION #useless, img, status=cropImage(impath, 'm', labelsfile, 21, 0.08) print("Image Loaded") img = make_square(img) img = augment(prep, img) print("Transformations done") img = img.transpose(-1, 0, 1).astype(np.float32) img = img.reshape(-1, CHANNELS, IMG_WIDTH, IMG_HEIGHT) # Inferencing starts here sess = onnxruntime.InferenceSession("./best_acc.onnx") print("The model expects input shape: ", sess.get_inputs()[0].shape) print("The shape of the Image is: ", img.shape) input_name = sess.get_inputs()[0].name result = sess.run(None, {input_name: img}) prob_array = result[0][0] print("Prob Array ", prob_array) prob = max(softmax(result[0][0])) print("Prob ", prob) species = tf.argmax(prob_array.ravel()[:20]).numpy() print("Class Label ", species) print("Spec ", CLASS_MAP[species][1]) string_label = CLASS_MAP[species][1].split(" ") #fullfilename = mosquitoid + "_" + picnum + "_" + string_label[0] + "_" + string_label[1] #fullbucket = 'photostakenduringpilotstudy' #s3_file = 'PilotStudy' #file = cv2.imwrite(fullfilename, originalimg) #upload_to_aws(file, fullbucket , s3_file) ##upload_file(file, fullbucket) return (string_label[0], string_label[1], str(prob), color_code(prob))
def step(self, inputs, states): uP_t = inputs vP_tm1 = states[0] _ = states[1:3] # ignore internal dropout/masks uQ, WQ_u, WP_v, WP_u, v, W_g1 = states[3:9] uQ_mask, = states[9:10] WQ_u_Dot = K.dot(uQ, WQ_u) #WQ_u WP_v_Dot = K.dot(K.expand_dims(vP_tm1, axis=1), WP_v) #WP_v WP_u_Dot = K.dot(K.expand_dims(uP_t, axis=1), WP_u) # WP_u s_t_hat = K.tanh(WQ_u_Dot + WP_v_Dot + WP_u_Dot) s_t = K.dot(s_t_hat, v) # v s_t = K.batch_flatten(s_t) a_t = softmax(s_t, mask=uQ_mask, axis=1) c_t = K.batch_dot(a_t, uQ, axes=[1, 1]) GRU_inputs = K.concatenate([uP_t, c_t]) g = K.sigmoid(K.dot(GRU_inputs, W_g1)) # W_g1 GRU_inputs = g * GRU_inputs vP_t, s = super(QuestionAttnGRU, self).step(GRU_inputs, states) return vP_t, s