Skip to content

Instantly share code, notes, and snippets.

numeric_columns.remove('consume')
ss = StandardScaler()
train_df[numeric_columns] = ss.fit_transform(train_df[numeric_columns])
val_df[numeric_columns] = ss.transform(val_df[numeric_columns])
# Splitting into train and val set -- 90-10 split
train_df, val_df = train_test_split(df, test_size = 0.1)
print("Number of samples in...")
print("Training set: ", len(train_df))
print("Validation set: ", len(val_df))
numeric_columns = ['distance','consume','temp_inside','refill liters'] # columns with ','
for column in numeric_columns:
#print(column)
x = [x.replace(',','.') for x in df[column].values]
df[column] = list(map(np.float32, x))
#including other numeric columns
numeric_columns.extend(['speed','temp_outside'])
df = pd.get_dummies(df, columns = ['refill gas'], dummy_na = True)
df = pd.get_dummies(df, columns = ['gas_type'], drop_first=True)
df = df.drop(columns = ['specials','refill gas_nan'], axis = 1)
df = df.sort_values('gas_type')
temp_inside = []
for t in df['gas_type'].unique():
temp_inside.extend(df[df['gas_type']==t]['temp_inside'].fillna(df[df['gas_type']==t]['temp_inside'].mode()[0]))
df['temp_inside'] = temp_inside
df['refill liters'] = df['refill liters'].fillna('0')
background = spectrogramOp(np.random.random((desired_samples)))
silence = spectrogramOp(np.zeros((desired_samples)))
background_out, silence_out = model.predict(np.array([background, silence]))
print("Predicted ", textLabel(background_out.argmax()), "on random audio with vector", background_out)
print("Predicted ", textLabel(silence_out.argmax()), "on null audio with vector", silence_out)
ind = int(np.random.uniform()*len(inputs_test))
spectrogram_out = inputs_test[ind]
ipd.Audio(spectrogram_out, rate=desired_sr)
y = labels_test[ind]
output = model.predict(np.expand_dims(np.array([inputs_test[ind]]), 0))
print("True label:", textLabel(np.argmax(y)))
print("Prediction:", textLabel(np.argmax(output)))
model = tf.keras.Sequential()
# (-1, 126, 65) = inputs_train.shape. Replace the numbers from inputs_train, if it changes.
# 126 = int(desired_samples/hop_len + 1), = time axis
# 65 = int(fft_len/2 + 1) = freq bins
lambda1 = tf.keras.layers.Lambda(lambda x: tf.reshape(x, (-1, int(fft_len/2 + 1), int(desired_samples/hop_len + 1), 1)),
name="add_channels", input_shape=(None, int(fft_len/2 + 1), int(desired_samples/hop_len + 1)))
conv2d1 = tf.keras.layers.Conv2D(16, (int(fft_len/2 + 1), 4), strides=1, activation='relu', name="conv1",
input_shape=(int(fft_len/2 + 1), int(desired_samples/hop_len + 1), 1))
#Selecting random index from train dataset
ind = int(np.random.uniform()*len(inputs_train))
#Displaying sample spectrogram and audio from train dataset
X = inputs_train[ind]
y = labels_train[ind].argmax()
print("Label :", textLabel(y) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.show()
total_len = input_labels.shape[0]
#Shuffling inputs and labels
shuffle_permutation = np.arange(total_len)
np.random.shuffle(shuffle_permutation)
input_spectrogram = input_spectrogram[shuffle_permutation]
input_labels = input_labels[shuffle_permutation]
#Splitting into train and test dataset - 90-10 ratio