Ejemplo n.º 1
0
    testNames: List[str] = specNames[numTrainSpectra:] * numVariations

t0 = time.time()
numSpecsTotal = len(trainSpectra) + len(testSpectra)

noisyTrainSpectra = distort.add_noise(trainSpectra, level=noiseLevel, seed=0)
noisyTestSpectra = distort.add_noise(testSpectra, level=noiseLevel, seed=numSpecsTotal)
for i in range(3):
    noisyTrainSpectra = distort.add_distortions(noisyTrainSpectra, level=noiseLevel*2, seed=i * numSpecsTotal)
    noisyTestSpectra = distort.add_ghost_peaks(noisyTestSpectra, level=noiseLevel*2, seed=2*i * numSpecsTotal)
    noisyTestSpectra = distort.add_distortions(noisyTestSpectra, level=noiseLevel*2, seed=2*i * numSpecsTotal)
    noisyTrainSpectra = distort.add_ghost_peaks(noisyTrainSpectra, level=noiseLevel*2, seed=i * numSpecsTotal)

print(f'Distorting spectra took {round(time.time()-t0, 2)} seconds')

trainSpectra = prepareSpecSet(trainSpectra, addDimension=False)
testSpectra = prepareSpecSet(testSpectra, addDimension=False)
noisyTrainSpectra = prepareSpecSet(noisyTrainSpectra, addDimension=False)
noisyTestSpectra = prepareSpecSet(noisyTestSpectra, addDimension=False)

rec: Reconstructor = getDenseReconstructor(dropout=0.0 if randomShuffle else 0.00)

t0 = time.time()
history = rec.fit(noisyTrainSpectra, trainSpectra,
                  epochs=10,
                  validation_data=(noisyTestSpectra, testSpectra),
                  batch_size=32, shuffle=True)
print(f"Training took {round(time.time()-t0, 2)} seconds.")

t0 = time.time()
reconstructedSpecs = rec.call(noisyTestSpectra)
Ejemplo n.º 2
0
specLength = 512
latent_dim = 64

folderName = os.path.basename(os.getcwd())
os.chdir(os.path.dirname(os.getcwd()))
noisySpecs, cleanSpecs, specNames, wavenumbers = io.load_microFTIR_spectra(
    specLength)
indicesPS = [i for i, name in enumerate(specNames) if name == 'PS']
indicesPP = [i for i, name in enumerate(specNames) if name == 'PP']

specsPS = noisySpecs[indicesPS]
specsPP = noisySpecs[indicesPP]

noisySpecs = noisySpecs[indicesPS]
dataset = prepareSpecSet(noisySpecs, transpose=False, addDimension=True)

numSynth = 300
generatorPS = load_model("Spectra Generation/PS Generator")
random_latent_vectors = tf.random.normal(shape=(numSynth, latent_dim))
generated_specs_ps = generatorPS(random_latent_vectors)
generated_specs_ps = generated_specs_ps.numpy()
generated_specs_ps = generated_specs_ps.reshape(
    (generated_specs_ps.shape[0], generated_specs_ps.shape[1]))

generatorPP = load_model("Spectra Generation/PP Generator")
random_latent_vectors = tf.random.normal(shape=(numSynth, latent_dim))
generated_specs_pp = generatorPP(random_latent_vectors)
generated_specs_pp = generated_specs_pp.numpy()
generated_specs_pp = generated_specs_pp.reshape(
    (generated_specs_pp.shape[0], generated_specs_pp.shape[1]))
Ejemplo n.º 3
0
specs = remapSpecArrayToWavenumbers(spectra, wavenumbers)
specs: np.ndarray = spectra[:, 1:]
trainSpectra = np.tile(specs, (1, numVariations))

t0 = time.time()
np.random.seed(42)
for i in range(3):
    noisyTrainSpectra = distort.add_noise(trainSpectra,
                                          level=noiseLevel * (i + 1) * 0.5)
    noisyTrainSpectra = distort.add_distortions(noisyTrainSpectra,
                                                level=noiseLevel * (i + 1) * 2)
    noisyTrainSpectra = distort.add_ghost_peaks(noisyTrainSpectra,
                                                level=noiseLevel * (i + 1) * 2)
print(f'Distorting spectra took {round(time.time()-t0, 2)} seconds')

trainSpectra = prepareSpecSet(trainSpectra)
testSpectra = prepareSpecSet(cleanSpecs, transpose=False)
noisyTrainSpectra = prepareSpecSet(noisyTrainSpectra)
noisyTestSpectra = prepareSpecSet(noisySpecs, transpose=False)

rec = getDenseReconstructor(dropout=0.5)
history = rec.fit(noisyTrainSpectra,
                  trainSpectra,
                  epochs=10,
                  validation_data=(noisyTestSpectra, testSpectra),
                  batch_size=32,
                  shuffle=True)

reconstructedSpecs = rec.call(noisyTestSpectra)
specPlot, boxPlot = out.getSpectraComparisons(testSpectra,
                                              noisyTestSpectra,
Ejemplo n.º 4
0
t0 = time.time()

noisySpecs, cleanSpecs, specNames, wavenumbers = io.load_microFTIR_spectra(SPECLENGTH, maxCorr=0.5)
numSpecs = noisySpecs.shape[0]
print(f'loading and remapping {numSpecs} spectra took {round(time.time()-t0)} seconds')
experimentTitle = 'MicroFTIR Spectra'

np.random.seed(42)
random.seed(42)
fracValid = 0.50
validationIndices: list = random.sample(range(numSpecs), round(numSpecs * fracValid))
valIndSet = set(validationIndices)
trainIndices: List[int] = [i for i in range(numSpecs) if i not in valIndSet]

trainSpectra = prepareSpecSet(cleanSpecs[trainIndices, :], transpose=False)
noisyTrainSpectra = prepareSpecSet(noisySpecs[trainIndices, :], transpose=False)

testSpectra = prepareSpecSet(cleanSpecs[validationIndices, :], transpose=False)
noisyTestSpectra = prepareSpecSet(noisySpecs[validationIndices, :], transpose=False)
print(f'{len(trainSpectra)} Specs for Training, {len(testSpectra)} Specs for Testing')

rec: Reconstructor = getDenseReconstructor(dropout=0.0)

t0 = time.time()
history = rec.fit(noisyTrainSpectra, trainSpectra,
                  epochs=200,
                  validation_data=(noisyTestSpectra, testSpectra),
                  batch_size=32, shuffle=True)
print(f"Training took {round(time.time()-t0, 2)} seconds.")