-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpneumonia.py
62 lines (33 loc) · 1.6 KB
/
pneumonia.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
# coding: utf-8
# ## Leitura das imagens
# In[1]:
from pyspark.ml.image import ImageSchema
from pyspark.sql.functions import lit
IMG_DIR = "chest_xray/"
train_df_normal = ImageSchema.readImages(IMG_DIR + "/train/NORMAL").withColumn("label", lit(0))
train_df_pneumonia = ImageSchema.readImages(IMG_DIR + "/train/PNEUMONIA").withColumn("label", lit(1))
train_df = train_df_normal.union(train_df_pneumonia)
test_df_normal = ImageSchema.readImages(IMG_DIR + "/test/NORMAL").withColumn("label", lit(0))
test_df_pneumonia = ImageSchema.readImages(IMG_DIR + "/test/PNEUMONIA").withColumn("label", lit(1))
test_df = test_df_normal.union(test_df_pneumonia)
# ## Treino do modelo
# In[ ]:
from sparkdl import DeepImageFeaturizer
from pyspark.ml import Pipeline
from pyspark.ml.classification import LogisticRegression
featurizer = DeepImageFeaturizer(inputCol="image", outputCol="features", modelName="InceptionV3")
lr = LogisticRegression(maxIter=20, regParam=0.05, elasticNetParam=0.3, labelCol="label")
pipeline = Pipeline(stages=[featurizer, lr])
model = pipeline.fit(train_df)
# ## Exibição do resultado por imagem
# In[ ]:
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
prediction = model.transform(test_df)
data_selection = prediction.select(prediction.image.origin, 'prediction')
data_selection.show(truncate=False)
# ## Exibição da Acurácia
# In[ ]:
prediction = model.transform(test_df)
prediction_and_labels = prediction.select('prediction', 'label')
evaluator = MulticlassClassificationEvaluator(metricName='accuracy')
print('Acurácia', str(evaluator.evaluate(prediction_and_labels)))