def test_latent_consistency_graph(): crf = LatentGraphCRF(n_labels=2, n_features=2, n_states_per_label=2) for i in xrange(10): w = np.random.normal(size=18) y = np.random.randint(2, size=(4)) x = np.random.normal(size=(4, 2)) e = np.array([[0, 1], [1, 2], [2, 0]], dtype=np.int) h = crf.latent((x, e), y, w) assert_array_equal(h / 2, y)
def test_latent_consistency_zero_pw_graph(): crf = LatentGraphCRF(n_labels=2, n_features=2, n_states_per_label=2) for i in xrange(10): w = np.zeros(18) w[:8] = np.random.normal(size=8) y = np.random.randint(2, size=(5)) x = np.random.normal(size=(5, 2)) h = crf.latent((x, np.zeros((0, 2), dtype=np.int)), y, w) assert_array_equal(h / 2, y)
def test_k_means_initialization_graph_crf(): # with only 1 state per label, nothing happends X, Y = generate_big_checker(n_samples=10) crf = LatentGraphCRF(n_states_per_label=1, n_features=2, n_labels=2) # convert grid model to graph model X = [(x.reshape(-1, x.shape[-1]), make_grid_edges(x, return_lists=False)) for x in X] H = crf.init_latent(X, Y) assert_array_equal(Y, H)
def test_loss_augmented_inference_energy_graph(): crf = LatentGraphCRF(n_labels=2, n_features=2, n_states_per_label=2, inference_method='lp') for i in range(10): w = np.random.normal(size=18) y = np.random.randint(2, size=(3)) x = np.random.normal(size=(3, 2)) e = np.array([[0, 1], [1, 2], [2, 0]], dtype=np.int) h_hat, energy = crf.loss_augmented_inference((x, e), y * 2, w, relaxed=True, return_energy=True) assert_almost_equal(-energy, np.dot(w, crf.joint_feature((x, e), h_hat)) + crf.loss(y * 2, h_hat))
X_train_, X_test_, X_train, X_test, y_train, y_test, y_org_train, y_org_test =\ train_test_split(X_, X, Y, y_org, test_size=.5) # First, perform the equivalent of the usual SVM. This is represented as # a CRF problem with no edges. pbl = GraphCRF(inference_method='unary') # We use batch_size=-1 as a binary problem can be solved in one go. svm = NSlackSSVM(pbl, C=1, batch_size=-1) svm.fit(X_train_, y_train) # Now, use a latent-variabile CRF model with SVM training. # 5 states per label is enough capacity to encode the 5 digit classes. latent_pbl = LatentGraphCRF(n_states_per_label=5, inference_method='unary') base_ssvm = NSlackSSVM(latent_pbl, C=1, tol=.01, inactive_threshold=1e-3, batch_size=10) latent_svm = LatentSSVM(base_ssvm=base_ssvm, latent_iter=2) latent_svm.fit(X_train_, y_train) print("Score with binary SVM:") print("Train: {:2.2f}".format(svm.score(X_train_, y_train))) print("Test: {:2.2f}".format(svm.score(X_test_, y_test))) print("Score with latent SVM:") print("Train: {:2.2f}".format(latent_svm.score(X_train_, y_train))) print("Test: {:2.2f}".format(latent_svm.score(X_test_, y_test)))
def trainModel_Latent(num_iter=5, latent_iter=3, inference="qpbo", trainer="NSlack", num_train=2, num_test=1, C=0.1, edges="180x180_dist1_diag0", inputs=[1, 1, 1, 1, 1, 1], features="all", directed=False, savePred=False): padding = (30, 30, 30, 30) if directed == True: features += '+directed' resultsDir = os.getcwd() + '/CRFResults' nameLen = len(os.listdir(resultsDir)) edgeFeature = edges filename = str(nameLen) + '_CRF_iter_' + str( num_iter ) + "_" + inference + "_" + trainer + "_" + features + "_" + str( num_train) + "_" + str(num_test) + "_" + edgeFeature print "Loading training slices" start = time.clock() train = extractSlices2(train_path, num_train, padding, inputs=inputs) end = time.clock() train_load_time = (end - start) / 60.0 [trainLayers, trainTruth, sliceShape] = train print "Training slices loaded in %f" % (train_load_time) n_features = len(trainLayers[0][0, 0]) print "Layer shape is : " print trainLayers[0].shape print "Training the model" edges = np.load("/home/bmi/CRF/edges/" + edges + ".npy") G = [edges for x in trainLayers] print trainLayers[0].shape trainLayers = np.array([ x.reshape((sliceShape[0] * sliceShape[1], n_features)) for x in trainLayers ]) trainTruth = np.array([ x.reshape((sliceShape[0] * sliceShape[1], )).astype(int) for x in trainTruth ]) if inference == 'ogm': crf = GraphCRF(inference_method=('ogm', { 'alg': 'fm' }), directed=directed) else: crf = LatentGraphCRF(n_states_per_label=2, inference_method=inference) if trainer == "Frank": base_svm = FrankWolfeSSVM(model=crf, max_iter=num_iter, C=C, n_jobs=-1, verbose=1) svm = LatentSSVM(base_ssvm=base_svm) elif trainer == "NSlack": base_svm = NSlackSSVM(model=crf, max_iter=num_iter, C=C, n_jobs=-1, verbose=1) svm = LatentSSVM(base_ssvm=base_svm, latent_iter=latent_iter) else: svm = OneSlackSSVM(model=crf, max_iter=num_iter, C=C, n_jobs=-1, verbose=1) start = time.clock() asdf = zip(trainLayers, G) svm.fit(asdf, trainTruth) end = time.clock() train_time = (end - start) / 60.0 print "The training took %f" % (train_time) print "Model parameter size :" print svm.w.shape print "making predictions on train data" predTrain = svm.predict(asdf) trainDice = [] for i in range(len(trainLayers)): diceScore = accuracy(predTrain[i], trainTruth[i]) trainDice.append(diceScore) meanTrainDice = sum(trainDice) / len(trainLayers) del trainLayers, trainTruth ################################################################################################ overallDicePerPatient = [] # For overall test Dice extDicePerPatient = [] PatientTruthLayers = [] PatientPredLayers = [] PREC = [] RECALL = [] F1 = [] LayerwiseDiceTotal = [] testResultFile = open(os.getcwd() + "/CRFResults/" + filename + ".csv", 'a') testResultFile.write( "folderName,numLayers, Overall Dice, precision , recall, F1" + "\n") counter = 0 print "Loading the test slices" for folder in os.listdir(test_path): path = test_path + "/" + folder layerDiceScores = '' # print path data = extractTestSlices2(path, padding, inputs=inputs) if data != 0: [testLayers, testTruth, sliceShape, startSlice, endSlice] = data # trueTestLayers=testLayers GTest = [edges for x in testLayers] testLayers = np.array([ x.reshape((sliceShape[0] * sliceShape[1], n_features)) for x in testLayers ]) testTruth = np.array([ x.reshape((sliceShape[0] * sliceShape[1], )).astype(int) for x in testTruth ]) asdfTest = zip(testLayers, GTest) predTest = svm.predict(asdfTest) LayerwiseDice = [] for i in range(len(testLayers)): diceScore = accuracy(predTest[i], testTruth[i]) layerDiceScores += "," + str(diceScore) if math.isnan(diceScore): if sum(predTest[i]) == 0 and sum(testTruth[i]) == 0: LayerwiseDice.append(1.0) continue LayerwiseDice.append(diceScore) LayerwiseDiceTotal.append(LayerwiseDice) overallTestDice = accuracy(np.hstack(predTest), np.hstack(testTruth)) extDice = np.mean( np.array(LayerwiseDice) [range(10) + range(len(LayerwiseDice) - 10, len(LayerwiseDice))]) prec, recall, f1 = precision_score(np.hstack(testTruth), np.hstack(predTest)), recall_score( np.hstack(testTruth), np.hstack(predTest)), f1_score( np.hstack(testTruth), np.hstack(predTest)) print "Patient %d : Overall test DICE for %s is : %f and extDice is %f" % ( counter, folder, overallTestDice, extDice) print "Precision : %f Recall : %f F1 : %f " % (prec, recall, f1) print "__________________________________________" # testResultFile.write(folder+","+str(len(testLayers))+","+str(meanTestDice)+","+str(overallTestDice) ","+str(np.max(testDice)) +","+ str(np.min(testDice))+"\n" ) testResultFile.write(folder + "," + str(len(testLayers)) + "," + str(overallTestDice) + "," + str(prec) + "," + str(recall) + "," + str(extDice) + layerDiceScores + "\n") overallDicePerPatient.append(overallTestDice) extDicePerPatient.append(extDice) PREC.append(prec), RECALL.append(recall), F1.append(f1) PatientTruthLayers.append(testTruth) PatientPredLayers.append(predTest) counter += 1 if counter == num_test and num_test != -1: break ###################################################################################################### print "Done testing slices" overallDice = sum(overallDicePerPatient) / len(PatientTruthLayers) overallPrec = sum(PREC) / len(PatientTruthLayers) overallRecall = sum(RECALL) / len(PatientTruthLayers) overallExtDice = np.mean(extDicePerPatient) print "Overall DICE : %f Precision : %f Recall : %f extDice : %f " % ( overallDice, overallPrec, overallRecall, overallExtDice) print "############################################" # testOutput=np.array([PatientPredLayers,PatientTruthLayers,trueTestLayers]) testOutput = np.array([PatientPredLayers, PatientTruthLayers]) ########### Saving the models ###################################################################### # print "Saving the model" # modelDir = os.getcwd()+"/CRFModel/" # svmModel = open(modelDir+filename+"_model"+".pkl",'wb') # cPickle.dump(svm,svmModel,protocol=cPickle.HIGHEST_PROTOCOL) # svmModel.close() # # print "saving the predictions" # predFileTest = open(os.getcwd()+"/CRFPred/"+filename+"_pred.pkl",'wb') # cPickle.dump(testOutput,predFileTest,protocol=cPickle.HIGHEST_PROTOCOL) # predFileTest.close() layerDataLog = open(os.getcwd() + "/CRFModel/" + filename + "_layer.pkl", 'wb') cPickle.dump(LayerwiseDiceTotal, layerDataLog, protocol=cPickle.HIGHEST_PROTOCOL) layerDataLog.close() resultLog = os.getcwd() + "/CRFResults/TestResultFinal.csv" resultFile = open(resultLog, 'a') resultFile.write(time.ctime() + "," + str(num_iter) + "," + str(num_train) + "," + str(num_test) + "," + inference + "," + trainer + "," + str(C) + "," + str(train_time) + "," + str(meanTrainDice) + "," + str(overallDice) + "," + str(np.std(overallDicePerPatient)) + "," + edgeFeature + "," + "None" + "," + features + "," + filename + "," + str(overallPrec) + "," + str(overallRecall) + "," + str(overallExtDice) + "," + "Flair+T2Latent with 2 states" + "\n") resultFile.close() testResultFile.close() return