def calculate_mIoU_accuracy(pred, labelBatch, labelIndexBatch, maskBatch, numClasses): ''' Calculage mIoU during training, given the temporary results during during code mainly borrowd from test.py ''' confcounts = np.zeros( (numClasses, numClasses), dtype=np.int64 ) accuracy = np.zeros(numClasses, dtype=np.float32 ) # Compute mean IOU hist = utils.computeAccuracy(pred, labelIndexBatch, maskBatch ) confcounts += hist for n in range(0, numClasses): rowSum = np.sum(confcounts[n, :] ) colSum = np.sum(confcounts[:, n] ) interSum = confcounts[n, n] accuracy[n] = float(100.0 * interSum) / max(float(rowSum + colSum - interSum ), 1e-5) meanAccuracy = np.mean(accuracy ) return meanAccuracy
labelIndex_cpu = dataBatch['labelIndex' ] labelIndexBatch.data.resize_(labelIndex_cpu.size() ) labelIndexBatch.data.copy_(labelIndex_cpu ) mask_cpu = dataBatch['mask' ] maskBatch.data.resize_( mask_cpu.size() ) maskBatch.data.copy_( mask_cpu ) # Test network x1, x2, x3, x4, x5 = encoder(imBatch ) pred = decoder(imBatch, x1, x2, x3, x4, x5 ) # Compute mean IOU loss = torch.mean( pred * labelBatch ) hist = utils.computeAccuracy(pred, labelIndexBatch, maskBatch ) confcounts += hist for n in range(0, opt.numClasses ): rowSum = np.sum(confcounts[n, :] ) colSum = np.sum(confcounts[:, n] ) interSum = confcounts[n, n] accuracy[n] = float(100.0 * interSum) / max(float(rowSum + colSum - interSum ), 1e-5) # Output the log information lossArr.append(loss.cpu().data.item() ) meanLoss = np.mean(np.array(lossArr[:] ) ) meanAccuracy = np.mean(accuracy ) print('Epoch %d iteration %d: Loss %.5f Accumulated Loss %.5f' \ % ( epoch, iteration, lossArr[-1], meanLoss ) )
def valid_model(in_path, da_path, sum_path,sess): #return accuracy for dialogue act, rouge-1,2,3,L for summary #some useful items are also calculated #da_outputs, correct_das: predicted / ground truth of dialogue act rouge_1 = [] rouge_2 = [] rouge_3 = [] rouge_L = [] da_outputs = [] correct_das = [] data_processor_valid = DataProcessor(in_path, da_path, sum_path, in_vocab, da_vocab) while True: #get a batch of data in_data, da_data, da_weight, length, sums, sum_weight,sum_lengths, in_seq, da_seq, sum_seq = data_processor_valid.get_batch(batch_size) feed_dict = {input_data.name: in_data, sequence_length.name: length, sum_length.name: sum_lengths} if data_processor_valid.end != 1 or in_data: ret = sess.run(inference_outputs, feed_dict) #summary part pred_sums = [] correct_sums = [] for batch in ret[1]: tmp = [] for time_i in batch: tmp.append(np.argmax(time_i)) pred_sums.append(tmp) for i in sums: correct_sums.append(i.tolist()) for pred,corr in zip(pred_sums,correct_sums): rouge_score_map = rouge.rouge(pred,corr) rouge1 = 100*rouge_score_map['rouge_1/f_score'] rouge2 = 100*rouge_score_map['rouge_2/f_score'] rouge3 = 100*rouge_score_map['rouge_3/f_score'] rougeL = 100*rouge_score_map['rouge_l/f_score'] rouge_1.append(rouge1) rouge_2.append(rouge2) rouge_3.append(rouge3) rouge_L.append(rougeL) #dialogue act part pred_das = ret[0].reshape((da_data.shape[0], da_data.shape[1], -1)) for p, t, i, l in zip(pred_das, da_data, in_data, length): p = np.argmax(p, 1) tmp_pred = [] tmp_correct = [] for j in range(l): tmp_pred.append(da_vocab['rev'][p[j]]) tmp_correct.append(da_vocab['rev'][t[j]]) da_outputs.append(tmp_pred) correct_das.append(tmp_correct) if data_processor_valid.end == 1: break precision = computeAccuracy(correct_das, da_outputs) logging.info('da precision: ' + str(precision)) logging.info('sum rouge1: ' + str(np.mean(rouge_1))) logging.info('sum rouge2: ' + str(np.mean(rouge_2))) logging.info('sum rouge3: ' + str(np.mean(rouge_3))) logging.info('sum rougeL: ' + str(np.mean(rouge_L))) data_processor_valid.close() return np.mean(rouge_1),np.mean(rouge_2),np.mean(rouge_3),np.mean(rouge_L),precision
maskBatch.data.copy_(mask_cpu) # Train network optEncoder.zero_grad() optDecoder.zero_grad() x1, x2, x3, x4, x5 = encoder(imBatch) pred = decoder(imBatch, x1, x2, x3, x4, x5) loss = torch.mean(pred * labelBatch) loss.backward() optEncoder.step() optDecoder.step() confcounts = utils.computeAccuracy(pred, labelIndexBatch, maskBatch) accuracy = np.zeros(opt.numClasses, dtype=np.float32) for n in range(0, opt.numClasses): rowSum = np.sum(confcounts[n, :]) colSum = np.sum(confcounts[:, n]) interSum = confcounts[n, n] accuracy[n] = float(100.0 * interSum) / max( float(rowSum + colSum - interSum), 1e-5) # Output the log information lossArr.append(loss.cpu().data.item()) accuracyArr.append(np.mean(accuracy)) if iteration >= 1000: meanLoss = np.mean(np.array(lossArr[-1000:])) meanAccuracy = np.mean(np.array(accuracyArr[-1000:]))