def extract_features_mfcc(input_path, class_name, train_test, class_names, bands, frames): window_size = 512 * (frames - 1) log_spectograms = [] labels = [] class_files = os.listdir(input_path + class_name + "/" + train_test) n_files = len(class_files) for i, aud_filename in enumerate(class_files): audio_path = input_path + class_name + "/" + train_test + "/" + aud_filename print("Preprocessing: " + class_name + "_" + train_test + ": " + str(i) + " of " + str(n_files) + " :" + class_name + "/" + train_test + "/" + aud_filename) audio_clip, sr = librosa.load(audio_path) for (start, end) in windows(audio_clip, window_size): if (len(audio_clip[start:end]) == int(window_size)): audio_signal = audio_clip[start:end] mel_spec = librosa.feature.melspectrogram(audio_signal, n_mels=bands) log_spec = librosa.logamplitude(mel_spec) log_spec = log_spec.T.flatten()[:, np.newaxis].T log_spectograms.append(log_spec) labels.append(encode_class(class_name, class_names)) log_specgrams = np.asarray(log_spectograms).reshape( len(log_spectograms), bands, frames, 1) features = np.concatenate( (log_specgrams, np.zeros(np.shape(log_specgrams))), axis=3) for i in range(len(features)): features[i, :, :, 1] = librosa.feature.delta(features[i, :, :, 0]) return np.array(features), np.array(labels)
def main(): H4Pt = tf.array([[0.1,0.1,0.2,0.2,0.3,0.3,0.4,0.4], tf.array([0.1,0.1,0.2,0.2,0.3,0.3,0.4,0.4]) * 2]) Perturbation = 10 OriginalCorners = tf.array([[5,10,1,2,5,10,1,2], tf.array([5,10,1,2,5,10,1,2])*2]) TensorDST(H4Pt, OriginalCorners, Perturbation, 2)
acc_test = np.mean(acc_tests) print("\rFinal test accuracy: {:.4f}% Loss: {:.6f}".format( acc_test * 100, loss_test)) '''Predictions''' n_samples = 5 sample_images = mnist.test.images[:n_samples].reshape([-1, 28, 28, 1]) with tf.Session() as sess: saver.restore(sess, checkpoint_path) caps2_output_value, decoder_output_value, y_pred_value = sess.run( [caps2_output, decoder_output, y_pred], feed_dict={X: sample_images, y: tf.array([], dtype=tf.int64)}) sample_images = sample_images.reshape(-1, 28, 28) reconstructions = decoder_output_value.reshape([-1, 28, 28]) plt.figure(figsize=(n_samples * 2, 3)) for index in range(n_samples): plt.subplot(1, n_samples, index + 1) plt.imshow(sample_images[index], cmap="binary") plt.title("Label:" + str(mnist.test.labels[index])) plt.axis("off") plt.show() plt.figure(figsize=(n_samples * 2, 3)) for index in range(n_samples):
def augment(pointcloud_inp, pointcloud_indices_0_inp, heatmapBatches, augmentation, numPoints=50000, numInputChannels=7): pointcloud_indices_inp = tf.zeros((FETCH_BATCH_SIZE, 6, NUM_POINTS), dtype='int32') newHeatmapBatches = [[] for heatmapIndex in xrange(len(heatmapBatches))] for imageIndex in xrange(pointcloud_inp.shape[0]): # pointcloud = pointcloud_inp[imageIndex] # pointcloud_indices_0 = pointcloud_indices_0_inp[imageIndex] # corner = corner_gt[imageIndex] # icon = icon_gt[imageIndex] # room = room_gt[imageIndex] # feature = feature_inp[imageIndex] # if 'w' in augmentation: # pointcloud_indices_0, [corner, icon, room, feature] = augmentWarping(pointcloud_indices_0, [corner, icon, room, feature], gridStride=32., randomScale=4) # pass # if 's' in augmentation: # pointcloud_indices_0, [corner, icon, room, feature] = augmentScaling(pointcloud_indices_0, [corner, icon, room, feature], randomScale=0) # pass # if 'f' in augmentation: # pointcloud_indices_0, [corner, icon, room, feature] = augmentFlipping(pointcloud_indices_0, [corner, icon, room, feature]) # pass # if 'd' in augmentation: # pointcloud, pointcloud_indices_0 = augmentDropping(pointcloud, pointcloud_indices_0, changeIndices=True) # pass # if 'p' in augmentation: # pointcloud, pointcloud_indices_0 = augmentDropping(pointcloud, pointcloud_indices_0, changeIndices=False) # pass # pointcloud_inp[imageIndex] = pointcloud # pointcloud_indices_inp[imageIndex] = getCoarseIndicesMaps(pointcloud_indices_0, WIDTH, HEIGHT, 0) # corner_gt[imageIndex] = corner # icon_gt[imageIndex] = icon # room_gt[imageIndex] = room # feature_inp[imageIndex] = feature newHeatmaps = [heatmapBatch[imageIndex] for heatmapBatch in heatmapBatches] if 'w' in augmentation: pointcloud_indices_0_inp[imageIndex], newHeatmaps = augmentWarping(pointcloud_indices_0_inp[imageIndex], newHeatmaps, gridStride=32, randomScale=4) pass if 's' in augmentation: pointcloud_inp[imageIndex], pointcloud_indices_0_inp[imageIndex], newHeatmaps = augmentScaling( pointcloud_inp[imageIndex], pointcloud_indices_0_inp[imageIndex], newHeatmaps) pass if 'f' in augmentation: pointcloud_inp[imageIndex], pointcloud_indices_0_inp[imageIndex], newHeatmaps = augmentFlipping( pointcloud_inp[imageIndex], pointcloud_indices_0_inp[imageIndex], newHeatmaps) pass if 'd' in augmentation: pointcloud_inp[imageIndex], pointcloud_indices_0_inp[imageIndex] = augmentDropping( pointcloud_inp[imageIndex], pointcloud_indices_0_inp[imageIndex], changeIndices=True) pass if 'p' in augmentation: pointcloud_inp[imageIndex], pointcloud_indices_0_inp[imageIndex] = augmentDropping( pointcloud_inp[imageIndex], pointcloud_indices_0_inp[imageIndex], changeIndices=False) pass # print(pointcloud_indices_0_inp[imageIndex].shape, pointcloud_indices_inp[imageIndex].shape) pointcloud_indices_inp[imageIndex] = getCoarseIndicesMaps(pointcloud_indices_0_inp[imageIndex], WIDTH, HEIGHT, 0) for heatmapIndex, newHeatmap in enumerate(newHeatmaps): newHeatmapBatches[heatmapIndex].append(newHeatmap) continue continue newHeatmapBatches = [tf.array(newHeatmapBatch) for newHeatmapBatch in newHeatmapBatches] pointcloud_inp = tf.concatenate([pointcloud_inp, tf.ones((FETCH_BATCH_SIZE, NUM_POINTS, 1))], axis=2) # print(pointcloud_itf.shape) # writePointCloud('test/pointcloud.ply', pointcloud_inp[0, :, :6]) # exit(1) if numPoints < pointcloud_itf.shape[1]: sampledInds = tf.range(pointcloud_itf.shape[1]) tf.random.shuffle(sampledInds) sampledInds = sampledInds[:numPoints] pointcloud_inp = pointcloud_inp[:, sampledInds] pointcloud_indices_inp = pointcloud_indices_inp[:, :, sampledInds] pass if numInputChannels == 4: pointcloud_inp = tf.concatenate([pointcloud_inp[:, :, :3], pointcloud_inp[:, :, 6:]], axis=2) pass return pointcloud_inp, pointcloud_indices_inp, newHeatmapBatches
def kp_iou(self, pred_kp, valid_true_kp): # [13, 13, 3, 14] pred_kp = pred_kp[..., 4:] # [V, 14] true_kp = valid_true_kp[..., 4:] # [13, 13, 3, 7, 2] pred_kp = tf.reshape(pred_kp, (13, 13, 3, 7, 2)) # [V, 7 ,2] true_kp = tf.reshape(valid_true_kp, (tf.shape(valid_true_kp)[0],7,2)) pred_kp_sorted = [] # need modification true_kp_sorted = [] # need modification for itr in range(tf.shape(pred_kp)[0]): corners = pred_kp[itr] n = tf.shape(corners)[0] cx = float(sum(x for x, y in corners)) / n cy = float(sum(y for x, y in corners)) / n cornersWithAngles = [] # need modification for x, y in corners: an = (tf.arctan2(y - cy, x - cx) + 2.0 * np.pi) % (2.0 * np.pi) cornersWithAngles.append((x, y, an)) cornersWithAngles.sort(key = lambda tup: tup[2]) # [N, 7, 2] pred_kp_sorted.append(list(starmap(lambda x, y, an: (x, y), cornersWithAngles))) pred_kp_sorted = tf.array(pred_kp_sorted) for itr in range(tf.shape(true_kp)[0]): corners = true_kp[itr] n = tf.shape(corners)[0] cx = float(sum(x for x, y in corners)) / n cy = float(sum(y for x, y in corners)) / n cornersWithAngles = [] # need modification for x, y in corners: an = (tf.arctan2(y - cy, x - cx) + 2.0 * np.pi) % (2.0 * np.pi) cornersWithAngles.append((x, y, an)) cornersWithAngles.sort(key = lambda tup: tup[2]) # [V, 7, 2] true_kp_sorted.append(list(starmap(lambda x, y, an: (x, y), cornersWithAngles))) true_kp_sorted = tf.array(true_kp_sorted) contours = map(tf.squeeze, pred_kp_sorted) # removing redundant dimensions pred_polygons = map(Polygon, contours) # converting to Polygons pred_multipolygon = MultiPolygon(pred_polygons) # putting it all together in a MultiPolygon contours = map(tf.squeeze, true_kp_sorted) # removing redundant dimensions true_polygons = map(Polygon, contours) # converting to Polygons true_multipolygon = MultiPolygon(true_polygons) # putting it all together in a MultiPolygon intersect_area = [] for i in range(tf.shape(pred_kp_sorted)[0]): intersect_area.append(pred_multipolygon[i].intersection(true_multipolygon[i]).area) intersect_area = tf.array(intersect_area) pred_area = [] true_area = [] for itr in range(tf.shape(pred_kp_sorted)[0]): corners = pred_kp_sorted[itr] n = tf.shape(corners)[0] area = 0.0 for i in range(n): j = (i + 1) % n area += corners[i][0] * corners[j][1] area -= corners[j][0] * corners[i][1] area = abs(area) / 2.0 pred_area.append(area) pred_area = tf.array(pred_area) for itr in range(tf.shape(true_kp_sorted)[0]): corners = true_kp_sorted[itr] n = tf.shape(corners)[0] area = 0.0 for i in range(n): j = (i + 1) % n area += corners[i][0] * corners[j][1] area -= corners[j][0] * corners[i][1] area = abs(area) / 2.0 true_area.append(area) true_area = tf.array(true_area) iou_kp = intersect_area / (pred_area + true_area - intersect_area) return iou_kp
""" python 行优先 for hang: for lie: for depth: """ import tensorflow as np import numpy as np a1 = np.array([1, 2, 3]) a2 = np.array([4, 5, 6]) a3 = np.array([7, 8, 9]) a4 = np.array([10, 11, 12]) a5 = np.array([13, 14, 15]) a6 = np.array([16, 17, 18]) A = np.zeros((2, 3, 3)) A[0, 0, :] = a1 A[0, 1, :] = a2 A[0, 2, :] = a3 A[1, 0, :] = a4 A[1, 1, :] = a5 A[1, 2, :] = a6 """ A: [[[ 1. 2. 3.] [ 4. 5. 6.] [ 7. 8. 9.]] [[10. 11. 12.] [13. 14. 15.]
class NeralNetwiork(object): def __init__(self): self.LayerNeuronCount=tf.constant([2,3,1]) self.LayerSize=tf.constant(len(self.LayerNeuronCount)) #weights self.W=tf.array([tf.ones([self.LayerNeuronCount[layernum-1],self.LayerNeuronCount[layernum]],dtype=object) for layernum in range(1,self.LayerSize)])
#소프트맥스 함수는 다음처럼 결과값을 전체합이 1인 확률로 만들어주는 함수 # 예) [8.04, 2.76, -6.52] ->[0.53, 0.24, 0.23] cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(model), axis=1)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) train_op = optimizer.minimize(cost) #비율 함수응 최소화시키면 -> 경사도를 0으로 만들어 가면 그 값이 최적화된 값일 것이가 #********** #신경망 학습 모델 #********** init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) for step in range(100): sess.run(train_op, {X: x_data, Y: y_data}) if (step + 1) % 10 == 10: print(step + 1, sess.run(cost, {X: x_data, Y: y_data})) #결과확인 prediction = tf.argmax(model, 1) target = tf.array(Y, 1) print("예측값", sess.run(prediction, {X: x_data})) print("실제값", sess.run(target, {Y: y_data})) is_correct = tf.equal(prediction, target) accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) print('정확도: %.2f' % sess.run(accuracy * 100, {X: x_data, Y: y_data}))