This repository has been archived by the owner on Sep 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepspectra.py
64 lines (57 loc) · 2.08 KB
/
deepspectra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Author: Gananath R
DeepSpecta: Spatial and temporal classification of sequence data
Contact: https://github.com/gananath
#Powder Xrd data downloaded from http://rruff.info/
"""
import pandas as pd
import numpy as np
import math as m
import copy
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.layers.convolutional import Conv2D,MaxPooling2D
from keras.layers.normalization import BatchNormalization
def padwithtens(vector, pad_width, iaxis, kwargs):
vector[:pad_width[0]] = 0
vector[-pad_width[1]:] = 0
return vector
def seq2tensor(data,width=None,height=None,pad=1):
if isinstance(data, pd.DataFrame):
data=data.values
if width==None or height==None:
#taking sqrt to make a nxn matrix
width=m.sqrt(data.shape[0])+1
height=m.sqrt(data.shape[0])+1
arr=[]
for i,j in enumerate(data.T):
d=copy.deepcopy(j)
#resizing
d.resize((width,height),refcheck=False)
#padding the edges of matrix with zeros
if pad!=0:
d=np.lib.pad(d, pad, padwithtens)
arr.append(d)
del i,j,d
return np.array(arr)
def cnn_model(epochs=250,lrate = 0.01):
# Create the model
model = Sequential()
model.add(Conv2D(97, (2, 2), input_shape=(2, 97, 97), padding='same', activation='tanh', kernel_constraint=maxnorm(3)))
model.add(BatchNormalization())
model.add(Dropout(0.8))
model.add(Conv2D(97, (2, 2),padding='same', activation='tanh', kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dropout(0.8))
model.add(Dense(512, activation='tanh', kernel_constraint=maxnorm(3)))
model.add(BatchNormalization())
model.add(Dropout(0.8))
model.add(Dense(7, activation='softmax'))
# Compile model
decay = lrate/epochs
sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model