我的问题与here相同,但是无法使其解决方案有效。
每次运行:
import tensorflow as tf
import tensorflow_datasets as tfds
class ExampleAugmentation:
def __init__(self, logdir: str, max_images: int, name: str):
self.file_writer = tf.summary.create_file_writer(logdir)
self.max_images: int = max_images
self.name: str = name
self._counter: int = 0
def __call__(self, image, label):
augmented_image = tf.image.random_flip_left_right(
tf.image.random_flip_up_down(image)
)
with self.file_writer.as_default():
tf.summary.image(
self.name,
augmented_image,
step=self._counter,
max_outputs=self.max_images,
)
self._counter += 1
return augmented_image, label
# Global settings
BATCH_SIZE = 32
DATASET_SIZE = 60000
EPOCHS = 5
LOG_DIR = "/logs/images"
AUGMENTATION = ExampleAugmentation(LOG_DIR, max_images=4, name="Images")
# Dataset
dataset = tfds.load("mnist", as_supervised=True, split=tfds.Split.TRAIN)
dataset = (
dataset.map(
lambda image, label: (
tf.image.convert_image_dtype(image, dtype=tf.float32),
label,
)
)
.batch(BATCH_SIZE)
.map(AUGMENTATION)
.repeat(EPOCHS)
)
# Model and training
model = tf.keras.models.Sequential(
[
tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation="softmax"),
]
)
model.compile(
optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
)
model.fit(
dataset,
epochs=EPOCHS,
steps_per_epoch=DATASET_SIZE // BATCH_SIZE,
callbacks=[tf.keras.callbacks.TensorBoard(log_dir=LOG_DIR)],
)
我得到:
线程1:致命错误:在隐式展开一个可选值时意外发现nil
CustomCell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = commentTableViewCell()
print("called T")
if arrayOfComments.count != 0 {
do {
let url = URL(string: (arrayOfComments[indexPath.row].user.profileImageUrlString)!)
let imageData = try Data(contentsOf: url!)
let image = UIImage(data: imageData)
cell.dateOfComment.text = "testText"//crash here
} catch {
print(error, ": Failed in do block converting image")
}
}
return cell
}
答案 0 :(得分:1)
设置您的单元格的标识符并替换
var myOutput = "is a string.";
使用
let cell = commentTableViewCell() // this loads the cell class without attached IB outlets
也是这个
let cell = tableView.dequeueReusableCell(withIdentifier:"cellId", for: indexPath) as! commentTableViewCell
阻止主线程
对于let imageData = try Data(contentsOf: url!)
中的Xib单元格
viewDidLoad
你必须有
commentTableViewCell.swift
commentTableViewCell.xib
对于原型单元格
只需在没有注册的情况下设置标识符,因为在这种情况下会自动完成